我已经成功地完成了以下工作:
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);
然而最后当我尝试添加第三个输出时,
transactions.Union(interactions);
// or
interactions.Union(transactions);
我收到以下错误
不管怎样,它都会抛出以下错误
错误 1 实例参数:无法从“System.Collections.Generic.List”转换为“System.Linq.IQueryable”
错误 2 'System.Collections.Generic.List' 不包含 'Union' 的定义和最佳扩展方法重载 'System.Linq.Queryable.Union(System.Linq.IQueryable, System.Collections.Generic.IEnumerable)' 有一些无效参数
第三个序列的唯一区别是,我使用与另一个表的连接。我努力了.AsEnumerable()
, .ToList()
and .ToArray()
,他们都没有帮助。
当你创建时interactions
,它的类型不是int和DateTime的匿名类型,它是int和nullable约会时间。这是因为在您的内联 if 语句中,您从未调用.Value
离开可为空的列。如果您创建,您的代码应该可以工作interactions
像这样:
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()};
更简单的例子:
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 语句在任何一种情况下都不会返回 null 值,但编译器无法做出这样的保证,并且必须在第二种情况下将返回值键入为可空 int 的返回值。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)