我有LINQ代码,我收到以下错误:System.ServiceModel.FaultException: The type 'ObjectMgmt' is not supported in aggregation operations.
(from cinnost in edc.CinnostSOPs
where cinnost.LegislativneVyznamna == true &&
cinnost.ObjektId == objektid
select (from o in edc.PlanRealizaces
where o.CinnostSOPIdSOP == cinnost.IdSOP &&
o.DatumPlatnosti <= DateTime.Now &&
o.Provest == true &&
o.DatumProvedeni == null
orderby o.DatumPlatnosti descending
select new ObjectMgmt
{
Datum = (DateTime.Now.Date - o.DatumPlatnosti.Value).TotalDays
}).Max(m => m)).ToList<ObjectMgmt>();
消息说的是聚合。我看到的唯一聚合是Max
调用。这是调试问题所需的提示。
你关心计算ObjectMgmt
实例序列的最大值,这显然是不可能的。
您得到的编译器错误告诉您ObjectMgmt
不能用作聚合的源。这是因为Max
要求ObjectMgmt
类型实现IComparable
。
在格式化查询以使其更可读之后,似乎您想找到Datum
具有最大值的ObjectMgmt
实例。
由于您已经对按DatumPlatnosti
降序的值排序,因此您知道ObjectMgmt
实例是通过增加Datum
值来排序的。因此,您根本不需要聚合。只取序列的最后一个元素(我将按升序排序,然后取第一个元素)。
(from cinnost in edc.CinnostSOPs
where cinnost.LegislativneVyznamna == true &&
cinnost.ObjektId == objektid
select (from o in edc.PlanRealizaces
where o.CinnostSOPIdSOP == cinnost.IdSOP &&
o.DatumPlatnosti <= DateTime.Now &&
o.Provest == true &&
o.DatumProvedeni == null
orderby o.DatumPlatnosti
select new ObjectMgmt
{
Datum = (DateTime.Now.Date - o.DatumPlatnosti.Value).TotalDays
}).First()).ToList<ObjectMgmt>();
因为您的ObjectMgmt
对象只有一个属性由查询填充:Datum
,更改您的Max
调用以获得Datum
的最大值,而不是ObjectMgmt
本身:
(from cinnost in edc.CinnostSOPs
where cinnost.LegislativneVyznamna == true &&
cinnost.ObjektId == objektid
select (from o in edc.PlanRealizaces
where o.CinnostSOPIdSOP == cinnost.IdSOP &&
o.DatumPlatnosti <= DateTime.Now &&
o.Provest == true &&
o.DatumProvedeni == null
orderby o.DatumPlatnosti descending
select new ObjectMgmt
{
Datum = (DateTime.Now.Date - o.DatumPlatnosti.Value).TotalDays
}).Max(m => m.Datum)).ToList<ObjectMgmt>();