Example #1
0
            protected virtual Expression VisitExtendedMemberInit(ExtendedMemberInitExpression node)
            {
                var newExpression = VisitAndConvert(node.NewExpression, nameof(VisitExtendedMemberInit));
                var arguments     = Visit(node.Arguments).ToArray();

                var entityType = model.FindEntityType(node.Type);

                if (entityType != null)
                {
                    var collectionMembers
                        = from n in entityType.GetNavigations()
                          where n.IsCollection()
                          from m in new[] { n.GetReadableMemberInfo(), n.GetWritableMemberInfo() }
                    select m;

                    for (var i = 0; i < arguments.Length; i++)
                    {
                        if (collectionMembers.Contains(node.WritableMembers[i]))
                        {
                            var sequenceType = node.WritableMembers[i].GetMemberType().GetSequenceType();

                            arguments[i]
                                = Expression.Coalesce(
                                      arguments[i].AsCollectionType(),
                                      Expression.New(typeof(List <>).MakeGenericType(sequenceType)));
                        }
                    }
                }

                return(node.Update(newExpression, arguments));
            }
Example #2
0
            protected virtual Expression VisitExtendedMemberInit(ExtendedMemberInitExpression node)
            {
                var newExpression = VisitAndConvert(node.NewExpression, nameof(VisitExtendedMemberInit));

                if (Finished)
                {
                    return(node.Update(newExpression, node.Arguments));
                }

                var arguments       = node.Arguments.ToList();
                var readableMembers = node.ReadableMembers.ToList();
                var writableMembers = node.WritableMembers.ToList();

                var currentMember     = path.Pop();
                var currentMemberInfo = currentMember.GetReadableMemberInfo();
                var foundMember       = false;

                for (var i = 0; i < arguments.Count; i++)
                {
                    var argument = arguments[i];

                    if (node.ReadableMembers[i] == currentMemberInfo)
                    {
                        foundMember = true;

                        if (path.Count == 0)
                        {
                            arguments[i] = includedExpression;
                            Finished     = true;
                        }
                        else
                        {
                            arguments[i] = Visit(argument);
                        }

                        break;
                    }
                }

                path.Push(currentMember);

                if (!foundMember && currentMemberInfo.DeclaringType.IsAssignableFrom(node.Type))
                {
                    Finished = true;

                    var writableMemberInfo = currentMember.GetWritableMemberInfo();

                    arguments.Add(
                        writableMemberInfo.GetMemberType().IsCollectionType()
                            ? includedExpression.AsCollectionType()
                            : includedExpression);

                    readableMembers.Add(currentMember.GetReadableMemberInfo());
                    writableMembers.Add(writableMemberInfo);

                    return(new ExtendedMemberInitExpression(
                               node.Type,
                               node.NewExpression,
                               arguments,
                               readableMembers,
                               writableMembers));
                }
                else
                {
                    return(node.Update(newExpression, arguments));
                }
            }