gpt4 book ai didi

NHibernate - 查询特定列并返回不同的记录?

转载 作者:行者123 更新时间:2023-12-03 12:20:29 25 4
gpt4 key购买 nike

我是 NH 的新手。

我在旧数据库中有一个表,如下所示:

Id,
CompanyId,
Description,
[LOADS of other columns here]

我想使用 NHibernate 返回一组 DISTINCT 数据,只选择特定的列并使用 WHERE 语句。 SQL 看起来像这样:
SELECT DISTINCT
[table_name].CompanyId,
[table_name].Description
FROM
[table_name]
WHERE
[table_name].CompanyId = 2

用谷歌搜索这个我想出了:
ProjectionList projections = Projections.ProjectionList();
projections.Add(Projections.Property("CompanyId"), "CompanyId");
projections.Add(Projections.Property("Name"), "SomeName");


var companyDto = session.QueryOver<Company>()
.Where(x => x.CompanyId == 2)
.Select(projections)
.TransformUsing(Transformers.AliasToBean<CompanyDto>())
.List<CompanyDto>();

if (companyDto != null)
Console.WriteLine(string.Format("{0}, {1}", companyDto.CompanyId, companyDto.SomeName));

DTO在哪里:
public class CompanyDto
{
public int CompanyId { get; set; }
public string SomeName { get; set; }
}

实体是:
public class Company
{
public virtual int Id { get; private set; }
public virtual int CompanyId { get; set; }
public virtual string Name { get; set; }
}

这不会带回无效的记录。我知道通常我必须使用不同的转换 (DistinctRootEntity),但我不能使用两个转换。如何将所有我想要的东西组合到一个电话中?它一定是可能的,它的基本 SQL ....

我需要:
  • 不使用 HQL
  • 不带回记录的所有列
  • 不带回重复的行
  • 最佳答案

    有一个投影

    var projections = Projections.Distinct(Projections.ProjectionList()
    .Add(Projections.Property("CompanyId").As("CompanyId"))
    .Add(Projections.Property("Name").As("SomeName"));

    关于NHibernate - 查询特定列并返回不同的记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6943355/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com