LINQ错误聚合操作中不支持该类型



我有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>();

最新更新