LINQ到实体联盟抛出错误 [英] LINQ to Entities Union is throwing an error
问题描述
我设法得到以下工作:
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
错误1实例参数:无法从转换'System.Collections.Generic.List< AnonymousType#1>'以System.Linq.IQueryable< AnonymousType#2 - ;'结果
错误2'System.Collections.Generic.List< AnonymousType#1>'不包含'联盟'的定义和最佳扩展方法超载System.Linq.Queryable.Union< TSource>(System.Linq.IQueryable< TSource>中System.Collections.Generic.IEnumerable< TSource>)'有一些无效参数
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
在第三个序列唯一的区别是,我使用与另一个表联接。我曾尝试 .AsEnumerable()
, .ToList()
和 .ToArray()
,他们没有帮助。
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.
推荐答案
在创建交互
,它的类型不是匿名int类型和日期,它是为int和可为空日期时间。这是因为在你的内联if语句,你永远不叫 .value的
关空列的。
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
虽然我们可以很容易地看出,内联if语句将永远不会返回空值在这两种情况下,编译器不能做出这种保证并有键入返回值,在第二种情况下一个可为空的int。
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.
这篇关于LINQ到实体联盟抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!