截取代码:
var systemList = scoList.FindAll(x => x.PServiceTypeID == serviceTypeId);
systemList = systemList.Distinct(new SystemIdComparer()).ToList();
systemList = systemList.OrderBy(x => x.PSystemDescription).ToList();
截取代码相关说明:
1、OrderBy 的相反方法是 OrderByDescending 传入参数一样;
2、SystemIdComparer:这个类要自己实现,例子:
public class SystemIdComparer : IEqualityComparer<SelectAssetSystemClassObjectEntity>
{
public bool Equals(SelectAssetSystemClassObjectEntity x, SelectAssetSystemClassObjectEntity y)
{
return x.PSystemID.Equals(y.PSystemID);
}
public int GetHashCode(SelectAssetSystemClassObjectEntity obj) { return 0; }
}
希望对您有所帮助。。。。O(∩_∩)O~
补充:Groupby
var groupedList = orderedList
.GroupBy(c => new {c.CustomerId, c.ProductId})
.OrderBy(g => g.Key.CustomerId)
.ThenBy(g => g.Key.ProductId)
.ToList();
foreach(var group in groupedList)
{
List<CustomerProduct> cps = group.ToList();
//do some work with this customer products
//no need to do inefficient list removal - just move on to next group.
}
另一种写法:
var customerAndProductGroups = from order in Orders
orderby order.CustomerID, order.ProductID // orderby not necessary, but neater
group order by new { order.CustomerID, order.ProductID };
这里几篇比较好的文章。