码迷,mamicode.com
首页 > Windows程序 > 详细

C# Distinct使用

时间:2016-04-05 09:29:35      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:

官网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

 

C# Distinct使用

标签:

原文地址:http://www.cnblogs.com/danlis/p/5353749.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!