• lambda distinct


    public ActionResult Index()
    {
    IList<RegisterModel> regList = new List<RegisterModel>()
    {
    new RegisterModel{ UserName ="zhangsan", Password="123456", ConfirmPassword ="123456"},
    new RegisterModel{ UserName ="li", Password="123456", ConfirmPassword ="123456"},
    new RegisterModel{ UserName ="zhangsan", Password="123456", ConfirmPassword ="123456"},
    new RegisterModel{ UserName ="wangwu", Password="123456", ConfirmPassword ="123456"},
    new RegisterModel{ UserName ="zhangsan", Password="123456", ConfirmPassword ="123456"}
    };
    List<RegisterModel> lambdaList = regList.Distinct(new Compare<RegisterModel>(
    (x, y) => (null != x && null != y) && (x.UserName == y.UserName))).ToList();


    return View();
    }
    public delegate bool EqualsComparer<T>(T x, T y);
    public class Compare<T> : IEqualityComparer<T>
    {
    private EqualsComparer<T> _equalsComparer;
    public Compare(EqualsComparer<T> equalsComparer)
    {
    this._equalsComparer = equalsComparer;
    }
    public bool Equals(T x, T y)
    {
    if (null != this._equalsComparer)
    return this._equalsComparer(x, y);
    else
    return false;
    }
    public int GetHashCode(T obj)
    {
    return obj.ToString().GetHashCode();
    }
    }

    //////////////////******************************////////

    var linqlist = (from r in regList
    group r by r.UserName into g
    select g).ToList();
    foreach (var group in linqlist)
    {
    // 输出组内成员
    foreach (var score in group)
    {
    Console.WriteLine(score);
    }
    }

  • 相关阅读:
    PP篇10 修改工单组件行
    取未清PO逻辑
    PP篇7 生产替代料齐套后处理
    PP篇9 更改计划订单
    DEBUG技巧里的问题1 双击某个变量不能显示
    HoloLens开发手记
    开始开发HoloLens应用吧 Start Developing HoloLens Apps Today
    HoloLens开发手记
    HoloLens开发手记
    HoloLens开发手记
  • 原文地址:https://www.cnblogs.com/niejun/p/4688600.html
Copyright © 2020-2023  润新知