0

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

3 回答 3

2

该消息谈到聚合。我看到的唯一聚合是Max调用。这是调试问题所需的提示。

您关心计算一系列ObjectMgmt实例的最大值,这显然是不可能的。把它改成你真正的意思。

于 2013-04-13T21:53:04.700 回答
1

您得到的编译器错误告诉您ObjectMgmt不能用作聚合的来源。发生这种情况是因为Max需要ObjectMgmt类型实现IComparable

在格式化查询以使其更具可读性之后,您似乎想要找到具有最大值的ObjectMgmt实例。Datum

由于您已经对值进行了降序排序,因此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>();
于 2013-04-13T23:23:57.323 回答
0

Because your ObjectMgmt objects have only one property filled by query: Datum, change your Max call to get max of Datum, not the ObjectMgmt itself:

(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>();
于 2013-04-14T07:27:56.007 回答