关于List<T> :Distinct 和 OrderBy 、GroupBy使用简例

 截取代码:

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



这里几篇比较好的文章。

Linq的Distinct太不给力了单列去重

LINQ TO SQL中的select

Linq中使用Left Join


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奋斗的小壁虎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值