LINQ to Entities Union is throwing an error

I have managed to get the following working:

var transactions = from t in context.Transactions
                   group t.Create_Date_Time by t.Participation_Id
                       into t1
                   select new { ParticipationId = t1.Key, CreateDateTime = t1.Max() };

var cases = from c in context.Cases
            group c.Create_Date_Time by c.Participation_Id
                into c1
            select new { ParticipationId = c1.Key, CreateDateTime = c1.Max() };

var interactions = from i in context.Interactions
                   join pp in context.Party_Participation on i.Party_Id equals pp.Party_Id
                   group i.Last_Update_Date_Time.HasValue ? i.Last_Update_Date_Time : i.Create_Date_Time
                       by pp.Participation_Id
                       into i1
                   select new { ParticipationId = i1.Key, CreateDateTime = i1.Max() };

transactions.Union(cases);

However at last when I was trying to add third output,

transactions.Union(interactions);
// or
interactions.Union(transactions);

I got the following error

either way it is throwing following error

Error 1 Instance argument: cannot convert from 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Linq.IQueryable<AnonymousType#2>'
Error 2 'System.Collections.Generic.List<AnonymousType#1>' does not contain a definition for 'Union' and the best extension method overload 'System.Linq.Queryable.Union<TSource>(System.Linq.IQueryable<TSource>, System.Collections.Generic.IEnumerable<TSource>)' has some invalid arguments

The only difference in third sequence is, I am using join with another table. I have tried .AsEnumerable() , .ToList() and .ToArray() , none of them helped.


When you create interactions , its type is not an anonymous type of int and DateTime, it is of int and nullable DateTime. This is because in your inline if statement, you never call .Value off of the nullable column. Your code should work if you create interactions like this:

var interactions = from i in context.Interactions
                   join pp in context.Party_Participation on i.Party_Id equals pp.Party_Id
                   group i.Last_Update_Date_Time.HasValue ? i.Last_Update_Date_Time.Value : i.Create_Date_Time by
                       pp.Participation_Id
                   into i1
                   select new {ParticipationId = i1.Key, CreateDateTime = i1.Max()};

Simpler Example:

var lst = new int?[] { 2,3,null,5,5 };
var lst2 = new int[] { 2,3,4,5,6 };

lst.Select(x => x.HasValue ? x.Value : 0).Union(lst2); //works fine
lst.Select(x => x.HasValue ? x : 0).Union(lst2); //throws error
lst.Select(x => x ?? 0).Union(lst2); //also works

Even though we can easily see that the inline if statement will never return a null value in either case, the compiler cannot make such guarantees and has to type the return value to that of a nullable int in the second case.


Most likely t1.Key and i1.Key are different types. The anonymous types must have exactly the same property names and types, declared in the same order for you to be able to union them.

As a side note, you probably also want to change this line of your code:

transactions.Union(cases);

to this:

var unionedQuery = transactions.Union(cases);

otherwise it won't perform the union, because you've not stored the resulting query.

链接地址: http://www.djcxy.com/p/75246.html

上一篇: 如何解决where子句项中的泛型谓词?

下一篇: LINQ to Entities Union正在抛出一个错误