官网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>();
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 项目中使用

C# Distinct使用,支持对象的相等比较的更多相关文章

  1. Spring cloud Feign不支持对象传参解决办法[完美解决]

    spring cloud 使用 Feign 进行服务调用时,不支持对象参数. 通常解决方法是,要么把对象每一个参数平行展开,并使用 @RequestParam 标识出每一个参数,要么用 @Reques ...

  2. SQL Server 新建 数据库关系图 时弹出警告提示此数据库没有有效所有者,因此无法安装数据库关系图支持对象。

    今天创建数据库关系图,发现提示此数据库没有有效所有者,因此无法安装数据库关系图支持对象.若要继续,请首先使用 数据库属性 对话框的文件页或 ALTER AUTHORIZAITION 语句将数据库所有者 ...

  3. springmvc 支持对象与json 自动转换的配置

    基于maven的工程, 需要在pom.xml中添加如下依赖 <dependency> <groupId>javax.servlet</groupId> <ar ...

  4. 1.部分(苹果)移动端的cookie不支持中文字符,2.从json字符串变为json对象时,只支持对象数组

    1.移动端的cookie不支持中文字符.可以用编码,解码的方式解决. 2.json字符串变成相应 的,json对象数组字符串.就这样 3.不同客户端(移动端.电脑)的请求,在C#服务端的取时间的格式竟 ...

  5. webpack-dev-server 多入口自动刷新,支持对象

    万物的来源~webpack 本身 watch webpack watch 传送门 webpack 可以监听文件变化,当它们修改后会重新编译 watch boolean 启用 Watch 模式.这意味着 ...

  6. distinct 数组去重,对象去重

    distinct 操作符可以用来去重,将上游重复的数据过滤掉. import { of } from 'rxjs'; import { distinct} from 'rxjs/operators'; ...

  7. javascript SDK开发之webpack打包支持对象展开运算符...

    场景:使用了对象的展开运算符,webpack打包报错,错误如下:Parsing error: Unexpected token - 1.安装依赖 npm install babel-preset-st ...

  8. 让jquery easyui datagrid列支持绑定嵌套对象

    嵌套对象是指返回的json数据,是对象的某个属性自带有属性.而我们恰恰又需要这个属性,默认情况下easyui的datagrid是不支持绑定嵌套对象的.比如:datagrid的field属性只能为fie ...

  9. 阿里云对象存储OSS支持版本管理特性

    阿里云对象存储OSS现已经全面支持“对象版本管理”特性.该功能适用于所有的存储类型以及区域.当Bucket启用该特性后,“对象版本管理”功能可以保护和恢复误删除.误覆盖的数据. 对象存储OSS“版本管 ...

随机推荐

  1. kubernetes外部访问的几种方式

    1:用的最多的是nodePort,如下nginx的service,将type设置成NodePort,同时nodePort设置成30010(k8s为了不与宿主机的端口冲突,默认限制了30000以下的端口 ...

  2. JEECG常见问题大全征集

    大家还有什么问题.请跟帖,谢谢支持. .  JEECG常见问题大全征集 1. jeecg没有数据库脚本问题   jeecg不须要数据库脚本,在数据库创建好数据库.项目配置好数据源链接.会自己主动建表. ...

  3. div的定位

    父DIV设置为相对定位,子DIV设置为绝对定位.<div style="height:158px; width:158px; position:relative;"> ...

  4. jquery对象和javascript的dom对象转换

    Jquery框架为jquery对象定义了独立使用的方法和属性,它无法直接调用dom对象的方法,dom对象也无法直接调用jquery对象的方法和属性. Jquery对象和dom对象是可以相互转换的,因为 ...

  5. [JS] selector 背景选择器

    用于listview和button改变android原来控件的背景 android的selector是在drawable/xxx.xml中配置的 1.定义xml 把下面的XML文件保存成你自己命名的. ...

  6. Hbase导入MapReduce数据的时候提示Running Job XXXX后就一直卡着不动

    代码确信无误之后,ant运行起来,发现一执行就卡在Running Job XXXX那里一直不动了. 试着把代码打包成jar扔到Linux执行也还是一样的效果.还是停在那里.然后就一顿瞎蒙.最后发现是H ...

  7. IE屏蔽鼠标右键、禁止复制粘贴等功能

    <body oncontextmenu="return false" onselectstart="return false" ondragstart=& ...

  8. e1087. 用For循环做数组的遍历

    The for statement can be used to conveninently iterate over the elements of an array. The general sy ...

  9. c# winform 自动关闭messagebox 模拟回车

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. (转)关于三星cortex A9 Sate4412 开发板 uboot 启动的一些问题释疑

    说明:本文转载自:http://blog.csdn.net/gooogleman/article/details/17238079  作者:gooogleman                   日 ...