【问题标题】:at least one object must implement icomparable. orderby descending C#至少一个对象必须实现 icomparable。降序 C#
【发布时间】:2019-11-01 20:48:21
【问题描述】:

我正在尝试通过其嵌套对象属性updatedDate 对自定义对象列表进行排序,但它会引发错误。我想根据嵌套属性字段PartsInformation.updatedDate 然后通过PartsDetail.updatedDate 订购商店列表。如果您需要更多详细信息,请告诉我,我会尝试更新它。我尝试使用Icomparable,但我是新手,所以我没有进一步进行。

类结构:

public class StoreHomeInfo
{
    public string StoreID { get; set; }
    public string StoreName { get; set; }
    public List<OperationalUnit> OperationalUnitData { get; set; }
}

public class OperationalUnit
{      
    public string OunitID { get; set; }
    public string OunitName { get; set; }
    public List<PartsInformation> PartData { get; set; }
    public List<PartsDetail> PartCost { get; set; }
}

public class PartsInformation
{
    public string PartID { get; set; }
    public string Partname { get; set; }
    public string Summary { get; set; }       
    public string StoreID { get; set; }
    public string OunitID { get; set; }        
    public DateTime? updatedDate { get; set; }
}

public class PartsDetail
{
    public string PartsDetailID { get; set; }
    public string PartDetailName { get; set; }
    public string Summary { get; set; }
    public string OunitID { get; set; }
    public string StoreID { get; set; }      
    public DateTime? updatedDate { get; set; }       
}

示例查询:

var result = (from st in lstobjStoreDetails
                group st by new { st.ID, st.Storename } into grouped
                    select new StoreHomeInfo()
                    {
                        StorID = grouped.Key.ID, 
                        StoreName = grouped.Key.Storename,
                        OperationalUnitData = (from ser in lstobjOpUnitDetails
                            where ser.StorID.Equals(grouped.Key.ID)
                            group ser by new { ser.ID, ser.Ounitname } into 
                            grouped1
                            select new OperationalUnit()
                            {
                                OunitID = grouped1.Key.ID,
                                OunitName = grouped1.Key.Ounitname,
                                PartsInformation = (from projes in lstobjpartDetails
                                      where projes.OunitID.Equals(grouped1.Key.ID)
                                      group serviceBS by new { projes.PartID, projes.Partname, projes.StorID, projes.OunitID,projes.updatedDate } into groupedparts
                                      select new PartsInformation()
                                      {
                                           PartID = lstobjpartDetails.Where(q => q.StorID == grouped.Key.ID && q.OunitID == grouped1.Key.ID && q.PartID ==  groupedparts.Key.PartID).FirstOrDefault() != null ? lstobjpartDetails.Where(q =>  q.StorID == grouped.Key.ID && q.OunitID == grouped1.Key._ID && q.PartID ==  groupedparts.Key.PartID).FirstOrDefault().PartID : null,
                                           Partname = groupedparts.Key.Partname,
                                           Summary = groupedparts.Key.Summary,
                                           OunitID = grouped1.Key.ID,
                                           StorID = grouped.Key.ID,
                                           updatedDate = Convert.ToDateTime(groupedparts.Key.updatedDate)
                                      }).ToList(),
                                PartCost = (from pes in lstobjpartCostDetails
                                            where pes.OunitID.Equals(grouped1.Key._ID)
                                            group serviceBS by new { pes.PartsDetailID, pes.PartDetailName, pes.OunitID, pes.Summary pes.updatedDate, pes.StorID} into grouped2
                                            select new PartsDetail()
                                            {
                                                PartsDetailID = lstobjpartCostDetails.Where(q => q.StorID == grouped.Key._ID && q.OunitID == grouped1.Key._ID && q.PartsDetailID == grouped2.Key.PartsDetailID).FirstOrDefault() != null ? lstobjpartCostDetails.Where(q => q.StorID == grouped.Key._ID && q.OunitID == grouped1.Key._ID && q.PartsDetailID == grouped2.Key.PartsDetailID).FirstOrDefault().PartsDetailID : null,
                                                PartDetailName = grouped2.Key.PartDetailName,
                                                Summary = grouped2.Key.Summary,
                                                StorID = grouped.Key.ID,
                                                OunitID = grouped1.Key.ID,
                                                updatedDate = Convert.ToDateTime(grouped2.Key.updatedDate)
                                            }).ToList()
                            }).ToList()
                    }).OrderByDescending(n => n.OperationalUnitData.OrderByDescending(p => p.PartCost != null && p.PartCost.Any() ? p.PartCost.OrderByDescending(q => q.updatedDate) : null)
                          .ThenBy(x => x.PartsInformation != null && x.PartsInformation.Any() ? x.PartsInformation.OrderByDescending(y => y.updatedDate) : null)).ToList();

【问题讨论】:

  • 什么错误是抛出?知道这一点对诊断问题大有帮助。
  • @JuliaHayward 错误消息:“至少一个对象必须实现 icomparable”这是我在错误消息中收到的唯一内容

标签: c# asp.net-mvc linq linq-to-entities


【解决方案1】:

这部分代码:

.OrderByDescending(p =&gt; p.PartCost != null &amp;&amp; p.PartCost.Any() ? p.PartCost.OrderByDescending(q =&gt; q.updatedDate) : null)

正在尝试订购OperationalUnits 的列表,但您订购的键是List&lt;PartCost&gt;。我怀疑您希望该列表中的一个成员来订购。

顺便说一句,您尝试在一行中完成大量工作,如果您分离出方法进入OrderByDescending 调用,您的代码会更加清晰。

【讨论】:

  • 我不想单独订购 Operation Unit,storeinfo 也是基于 PartsInformation 和 Part Cost 更新日期的循环排序,目标是根据其嵌套子项提供最近的商店信息列表(零件信息,零件成本),但感谢您的关注
猜你喜欢
  • 2021-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-10
  • 2015-06-10
  • 1970-01-01
相关资源
最近更新 更多