Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在我的NHibernate查询中只选择几列?_C#_Nhibernate - Fatal编程技术网

C# 如何在我的NHibernate查询中只选择几列?

C# 如何在我的NHibernate查询中只选择几列?,c#,nhibernate,C#,Nhibernate,我有一个一个类到一个表的映射;不幸的是,这个表有110多个列,查询需要很长的时间,特别是当我大部分时间只想查看时,请使用ProjectionList来选择所需的列。对于示例。使用LINQ很容易(假设您使用的是NHibernate 3.0或更高版本): 然后您的查询: string hql = "select p.ProductName as Name, p.ShortDesc as Description ...(snip) " + "from Product p "

我有一个一个类到一个表的映射;不幸的是,这个表有110多个列,查询需要很长的时间,特别是当我大部分时间只想查看时,请使用
ProjectionList
来选择所需的列。对于示例。

使用LINQ很容易(假设您使用的是NHibernate 3.0或更高版本):

然后您的查询:

string hql = "select p.ProductName as Name, p.ShortDesc as Description ...(snip) " +
             "from Product p " +
             "where ...some query (snip)";

IQuery query = Session.CreateQuery(hql)
    .SetResultTransformer(Transformers.AliasToBean<ProductReport>());

IList<ProductReport> products = query.List<ProductReport>();
string hql=“选择p.ProductName作为名称,选择p.ShortDesc作为描述…(snip)”+
“来自产品p”+
“哪里…一些疑问(剪报)”;
IQuery=Session.CreateQuery(hql)
.SetResultTransformer(Transformers.AliasToBean());
IList products=query.List();

只需确保查询中的别名(如名称、说明等)与类中的属性名称匹配。

除了Tim给出的示例外,您还可以执行以下操作:

IList selection =
    session.QueryOver<Cat>()
        .Select(
            c => c.Name,
            c => c.Age)
        .List<object[]>();
IList选择=
session.QueryOver()
.选择(
c=>c.Name,
c=>c.Age)
.List();

上面的例子取自:

你说的110列是什么?回到绘图板上@V4Vendetta“什么?重新设计我宝贵的数据库?我花了数年时间来完善它的结构,没有人会告诉我如何处理它!!!”“(c)当我有100多个潜在的列/属性与任何组合时,你的客户……帮不上忙。不管怎样,谢谢。如果链接死了,答案就没用了。包括一个小的但重要的片段和链接。。。
string hql = "select p.ProductName as Name, p.ShortDesc as Description ...(snip) " +
             "from Product p " +
             "where ...some query (snip)";

IQuery query = Session.CreateQuery(hql)
    .SetResultTransformer(Transformers.AliasToBean<ProductReport>());

IList<ProductReport> products = query.List<ProductReport>();
IList selection =
    session.QueryOver<Cat>()
        .Select(
            c => c.Name,
            c => c.Age)
        .List<object[]>();