我正在尝试通过其嵌套对象属性updatedDate
对Custom对象的列表进行排序,但是会引发错误。我想基于嵌套属性字段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();
答案 0 :(得分:0)
这部分代码:
sleeper-d75b55fc9-swkj9
试图订购.OrderByDescending(p => p.PartCost != null && p.PartCost.Any() ? p.PartCost.OrderByDescending(q => q.updatedDate) : null)
的列表,但是订购的依据是OperationalUnit
。我怀疑您要订购该列表的成员。
顺便说一句,您正在尝试在一行中做很多工作,如果您将方法分开进行List<PartCost>
调用,则代码将更加清晰。