void Init()
            {
                var info1 = _sequence1.ConvertToIndex(null, 0, ConvertFlags.All).ToList();
                var info2 = _sequence2.ConvertToIndex(null, 0, ConvertFlags.All).ToList();

                if (!_isObject)
                {
                    return;
                }

                var unionMembers = new List <UnionMember>();

                foreach (var info in info1)
                {
                    if (info.MemberChain.Length == 0)
                    {
                        throw new InvalidOperationException();
                    }

                    var mi = info.MemberChain.First(m => m.DeclaringType !.IsSameOrParentOf(_unionParameter !.Type));

                    var member = new Member
                    {
                        SequenceInfo     = info,
                        MemberExpression = Expression.MakeMemberAccess(_unionParameter, mi)
                    };

                    unionMembers.Add(new UnionMember {
                        Member = member, Info1 = info
                    });
                }

                foreach (var info in info2)
                {
                    if (info.MemberChain.Length == 0)
                    {
                        throw new InvalidOperationException();
                    }

                    var em = unionMembers.FirstOrDefault(m =>
                                                         m.Member.SequenceInfo != null &&
                                                         m.Info2 == null &&
                                                         m.Member.SequenceInfo.CompareMembers(info));

                    if (em == null)
                    {
                        em = unionMembers.FirstOrDefault(m =>
                                                         m.Member.SequenceInfo != null &&
                                                         m.Info2 == null &&
                                                         m.Member.SequenceInfo.CompareLastMember(info));
                    }

                    if (em == null)
                    {
                        var member = new Member {
                            MemberExpression = Expression.MakeMemberAccess(_unionParameter, info.MemberChain[0])
                        };

                        if (_sequence2.IsExpression(member.MemberExpression, 1, RequestFor.Object).Result)
                        {
                            throw new LinqException("Types in {0} are constructed incompatibly.", _methodCall.Method.Name);
                        }

                        unionMembers.Add(new UnionMember {
                            Member = member, Info2 = info
                        });
                    }
                    else
                    {
                        em.Info2 = info;
                    }
                }
Beispiel #2
0
            void Init()
            {
                var info1 = _sequence1.ConvertToIndex(null, 0, ConvertFlags.All).ToList();
                var info2 = _sequence2.ConvertToIndex(null, 0, ConvertFlags.All).ToList();

                if (!_isObject)
                {
                    return;
                }

                var unionMembers = new List <UnionMember>();

                foreach (var info in info1)
                {
                    if (info.MemberChain.Count == 0)
                    {
                        throw new InvalidOperationException();
                    }

                    var mi = info.MemberChain.First(m => m.DeclaringType.IsSameOrParentOf(_unionParameter !.Type));

                    var member = new Member
                    {
                        SequenceInfo     = info,
                        MemberExpression = Expression.MakeMemberAccess(_unionParameter, mi)
                    };

                    unionMembers.Add(new UnionMember {
                        Member = member, Info1 = info
                    });
                }

                foreach (var info in info2)
                {
                    if (info.MemberChain.Count == 0)
                    {
                        throw new InvalidOperationException();
                    }

                    var em = unionMembers.FirstOrDefault(m =>
                                                         m.Member.SequenceInfo != null &&
                                                         m.Info2 == null &&
                                                         m.Member.SequenceInfo.CompareMembers(info));

                    if (em == null)
                    {
                        em = unionMembers.FirstOrDefault(m =>
                                                         m.Member.SequenceInfo != null &&
                                                         m.Info2 == null &&
                                                         m.Member.SequenceInfo.CompareLastMember(info));
                    }

                    if (em == null)
                    {
                        var member = new Member {
                            MemberExpression = Expression.MakeMemberAccess(_unionParameter, info.MemberChain[0])
                        };

                        if (_sequence2.IsExpression(member.MemberExpression, 1, RequestFor.Object).Result)
                        {
                            throw new LinqException("Types in {0} are constructed incompatibly.", _methodCall.Method.Name);
                        }

                        unionMembers.Add(new UnionMember {
                            Member = member, Info2 = info
                        });
                    }
                    else
                    {
                        em.Info2 = info;
                    }
                }

                var aliases1 = _sequence1.SelectQuery.Select.Columns.ToLookup(c => c.Expression, c => c.Alias);
                var aliases2 = _sequence2.SelectQuery.Select.Columns.ToLookup(c => c.Expression, c => c.Alias);

                _sequence1.SelectQuery.Select.Columns.Clear();
                _sequence2.SelectQuery.Select.Columns.Clear();

                for (var i = 0; i < unionMembers.Count; i++)
                {
                    var member = unionMembers[i];

                    if (member.Info1 == null)
                    {
                        var type = unionMembers.First(m => m.Info1 != null).Info1 !.MemberChain.First().GetMemberType();
                        member.Info1 = new SqlInfo(member.Info2 !.MemberChain)
                        {
                            Sql   = new SqlValue(type, null),
                            Query = _sequence1.SelectQuery,
                        };

                        member.Member.SequenceInfo = member.Info1;
                    }

                    if (member.Info2 == null)
                    {
                        var spam = unionMembers.First(m => m.Info2 != null).Info2 !.MemberChain.First();
                        var type = spam.GetMemberType();

                        member.Info2 = new SqlInfo(member.Info1.MemberChain)
                        {
                            Sql   = new SqlValue(type, null),
                            Query = _sequence2.SelectQuery,
                        };
                    }