NHibernate查询概述

4

我正在尝试使用以下代码从我的表中仅选择一些字段。

IList<Product> res = sess.QueryOver<Product>()            
             .Select(x =>x.name)           
             .List<Product>();

这段代码没有任何错误,但在运行时我得到了以下报错信息:"无法执行查找[SQL: SQL不可用]",并且提示"Prod1不是SympleFlhLINQ.Product类型的变量,不能在此泛型集合中使用".

如果有人能告诉我如何仅获取产品名称和相关类别名称,像这样的内容,那就太好了。

 IList<Product> res = sess.QueryOver<Product>() 
                .Select(x =>x.name)
                .Select(x=>x.Cat.CategoryName)
                .List<Product>();

你需要一个 IList<Product> 还是可以使用 DTO? - Andrew Whitaker
DTO是指数据传输对象,您的意思是使用类似于public class ProductCollection : List<Product>的类型吗? - maxs87
1个回答

9
IList<string> names = sess.QueryOver<Product>()            
         .Select(x =>x.Name)
         .List<string>();

或者

ProductDto product = null;
Category category = null;
IList<ProductDto> res = sess.QueryOver<Product>()
    .JoinAlias(x => x.Category, () => category)
    .SelectList(list => list
        .Select(x => x.Name).WithAlias(() => product.Name)
        .Select(() => category.Name).WithAlias(() => product.CategoryName))
    .TransformUsing(Transformers.AliasToBean<ProductDto>())
    .List<ProductDto>();

我是Stack Overflow的新手,不知道如何接受答案,哈哈。抱歉。 - maxs87

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接