.net - Linq 到 NHibernate : Distinct

标签 .net sql linq-to-nhibernate

我正在尝试使用 Linq-to-NHibernate 获取以下 SQL 输出:

SELECT DISTINCT Name, at.Year FROM MyTable mt
INNER JOIN AnotherTable at ON at.Id = mt.AnotherTableId

Name 和 Year 属性将包含在一个新类中,因此 C# 代码将如下所示:
Session.Linq()
   .Select(x => new FooBar { Name = x.Name, Year = x.AnotherTable.Year }))
   .ToList();

如何让 DISTINCT 关键字出现在 sql 查询中?

最佳答案

你不能试试:

Session.Linq()
   .Select(x => new FooBar { Name = x.Name, Year = x.Year }))
   .Distinct()
   .ToList();
Select返回 IEnumerable ,所以默认情况下它应该有 Distinct ,无论您的智能感知是否检测到它。

关于.net - Linq 到 NHibernate : Distinct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1740028/

相关文章:

c# - C#、SQL 2005、VS 2008 中的 "Object reference not set to an instance of an object."

c# - 在列表中查找一个属性包含重复值的对象

sql - 区分 SQL ID、个人用户和 AD 组

.net - KeyBinding 两个命令(一个在 VM 中,一个在 View 中)到一个键

MYSQL:复制列但更改类型

NHibernate 使用 Linq 或 QueryOver 将子实体投影到父属性中

c# - nHibernate 按年和月分组计数

c# - 如何从其父级返回集合的第一个对象

.net - 如何添加重试以调用 Web 服务?

c# - 应用程序默认凭据不可用。如果在 .NET C# 中的 Google Compute Engine 中运行,则它们可用