简体   繁体   中英

at least one object must implement icomparable. orderby descending C#

I'm trying to order the list of Custom object by its nested object property updatedDate but it throws error. I want to order the Store list based on nested property field PartsInformation.updatedDate then by PartsDetail.updatedDate . If you need more details, let me know and I will try to update it. I tried with Icomparable but I'm new to that so I didn't proceed further.

Class Structure:

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; }       
}

Sample Query :

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

This part of the code:

.OrderByDescending(p => p.PartCost != null && p.PartCost.Any() ? p.PartCost.OrderByDescending(q => q.updatedDate) : null)

is trying to order a list of OperationalUnit s, but the key on which you're ordering by is a List<PartCost> . I suspect you want a member of that list to order by.

As an aside, you're trying to do a lot of work in one line and your code would be far clearer if you separated out methods to go into the OrderByDescending calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM