LINQ to Entities - DISTINCT在一列 [英] LINQ to Entities - DISTINCT on one column

查看:72
本文介绍了LINQ to Entities - DISTINCT在一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  SELECT DISTINCT Column1 
FROM Table1

我正在使用Entity Framework 4.当然,我不想使用Distinct方法来从数据库中提取数据后过滤数据。



谢谢, Pawel

解决方案

使用类似

  db.Table1.Select(t => t.Column1).Distinct()

正如Munim在他的评论中提到的,Distinct()方法确实将DISTINCT添加到查询中。所以结果SQL查询将是

  SELECT [Distinct1]。[Column1] AS [Column1] 
FROM(SELECT DISTINCT
[Extent1]。[Column1] AS [Column1]
FROM [dbo]。[Table1] AS [Extent1]
)AS [Distinct1]


Is there a way to write following query with Linq to Entities (or Entity SQL, or Method Syntax, or any other way, but I would like to achieve it with Linq to Entities):

SELECT DISTINCT Column1
FROM Table1

I'm using Entity Framework 4. Of course I don't want to use Distinct method that filters data after data is fetched from database.

thanks,Pawel

解决方案

Use something like

db.Table1.Select(t => t.Column1).Distinct()

As Munim mentioned in his comment, the Distinct() method does add the DISTINCT to the query. So resulting SQL query will be

SELECT [Distinct1].[Column1] AS [Column1]
  FROM ( SELECT DISTINCT 
    [Extent1].[Column1] AS [Column1]
    FROM [dbo].[Table1] AS [Extent1]
  )  AS [Distinct1]

这篇关于LINQ to Entities - DISTINCT在一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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