获得使用LINQ to实体重复的记录 [英] Get distinct records using linq to entity

查看:192
本文介绍了获得使用LINQ to实体重复的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我使用LINQ to实体在我的应用程序。我需要根据一列值名称



所以,我也有类似的一个表像,你可以看到下面来获得不同的记录:

 (用户)
ID
名称
乡村
dateCreated会

我需要根据名称(唯一的)选择所有这项,但唯一身份。是否有可能实现使用LINQ,如果是的话请告诉我怎么了。

  VAR项目=(从我在用户选择新{ i.id,i.name,i.country,i.datecreated})是不同的()。 


解决方案

鲜明的()方法不能很好地执行,因为它没有鲜明的SQL谓词发送到数据库。使用而不是:

  VAR distinctResult =从C的结果
组C由c.Id到uniqueIds
选择uniqueIds.FirstOrDefault();



LINQ的集团实际上创建由酒店键控实体分组您指示:

 史密斯
约翰
玛丽
埃德
琼斯
杰里
鲍勃
萨利

的语法上述返回键,从而产生不同的列表。点击此处了解详情:



http://imar.spaanjaars.com/546/using-grouping-instead-of-distinct-in-entity-framework-to-optimize-performance


Hi I'm using linq to entity in my application. I need to get distinct records based on one column value "Name"

So I have a table similar like you can see below:

(User)
ID
Name
Country
DateCreated

I need to select all this items but uniques based on Name (unique). Is it possible to accomplish using linq, if so please show me how.

var items = (from i in user select new  {i.id, i.name, i.country, i.datecreated}).Distinct();

解决方案

The Distinct() method doesn't perform well because it doesn't send the DISTINCT SQL predicate to the database. Use group instead:

var distinctResult = from c in result
             group c by c.Id into uniqueIds
             select uniqueIds.FirstOrDefault();

LINQ's group actually creates subgroups of entities keyed by the property you indicate:

Smith
  John
  Mary
  Ed
Jones
  Jerry
  Bob
  Sally

The syntax above returns the keys, resulting in a distinct list. More information here:

http://imar.spaanjaars.com/546/using-grouping-instead-of-distinct-in-entity-framework-to-optimize-performance

这篇关于获得使用LINQ to实体重复的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆