zoukankan      html  css  js  c++  java
  • C# Distinct使用,支持对象的相等比较

    官网Enumerable.Distinct

    https://msdn.microsoft.com/zh-cn/library/bb338049.aspx

    CSDN中作者oriency755

    关于Distinct的使用:

    http://blog.csdn.net/oriency755/article/details/13773557

    使用

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Transactions;
    
    namespace Activity
    {
        public class ActivitySceneService
        {
            public List<DrawPlay> GetDraw(int activitySceneID)
            {
                using (var dbContext = new DbContext())
                {
                    var merchant = dbContext.Find<Merchant>(1);
                    var playList = new List<DrawPlay>();
                        playList = dbContext.Draw001Plays.Where(u => u.ActivitySceneID == activitySceneID).Distinct(
                            new Compare<DrawPlay>((x, y) => (x != null && y != null && x.UserID == y.UserID))).ToList();//放置比较器
                    return playlist;
                }
            }
        }
    
        public delegate bool CompareDelegate<T>(T x, T y);
        public class Compare<T> : IEqualityComparer<T>
        {
            private CompareDelegate<T> _compare;
            public Compare(CompareDelegate<T> d)
            {
                this._compare = d;
            }
    
            public bool Equals(T x, T y)
            {
                if (_compare != null)
                {
                    return this._compare(x, y);
                }
                else
                {
                    return false;
                }
            }
    
            public int GetHashCode(T obj)
            {
                return obj.ToString().GetHashCode();
            }
        }
    }
    Test
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    namespace NAMESPACE
    {
        public class CLASSNAME
        {
            public void Test()
            {
                using (var dbContext = new DbContext())
                {
                     dbContext.DATABASE.Distinct(
                            new Compare<MODELTYPE>((x, y) => (x != null && y != null && x.FIELD> VALUE && y.FIELD> VALUE))).ToList();
                }
            }
         }
    
    //使用委托
        public delegate bool CompareDelegate<T>(T x, T y);
        public class Compare<T> : IEqualityComparer<T>
        {
            private CompareDelegate<T> _compare;
            public Compare(CompareDelegate<T> d)
            {
                this._compare = d;
            }
    
            public bool Equals(T x, T y)
            {
                if (_compare != null)
                {
                    return this._compare(x, y);
                }
                else
                {
                    return false;
                }
            }
    
            public int GetHashCode(T obj)
            {
                return obj.ToString().GetHashCode();
            }
        }
    }
    和上一个差不多
    Distinct项目内使用
    
    using Qxun.Framework.Utility
    
    var onlineRecords = dbContext.OnLineRecords.ToList().Distinct(new CompareExtend<OnLineRecord>((x, y) => x != null && y != null && x.MerchantID == y.MerchantID && x.ActivitySceneID == y.ActivitySceneID));
    
    ****************************************CompareExtend类********************************************************
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Qxun.Framework.Utility
    {
        public delegate bool CompareDelegate<T>(T x, T y);
        public class CompareExtend<T> : IEqualityComparer<T>
        {
            private CompareDelegate<T> _compare;
            public CompareExtend(CompareDelegate<T> d)
            {
                this._compare = d;
            }
    
            public bool Equals(T x, T y)
            {
                if (_compare != null)
                {
                    return this._compare(x, y);
                }
                else
                {
                    return false;
                }
            }
    
            public int GetHashCode(T obj)
            {
                return obj.ToString().GetHashCode();
            }
        }
    }
    distinct 项目中使用
  • 相关阅读:
    Java Output流写入包装问题
    SpringBoot项目单元测试不经过过滤器问题
    SpringSecurity集成启动报 In the composition of all global method configuration, no annotation support was actually activated 异常
    JWT jti和kid属性的说明
    Maven 排除依赖
    第五章 基因概念的发现
    第三章 孟德尔遗传的拓展
    第二章 孟德尔遗传
    第一章 引言
    GWAS全基因组关联分析
  • 原文地址:https://www.cnblogs.com/danlis/p/5353749.html
Copyright © 2011-2022 走看看