gpt4 book ai didi

c# - LINQ 的转换问题

转载 作者:行者123 更新时间:2023-11-30 22:46:28 24 4
gpt4 key购买 nike

我正在尝试使用 Linq 将我的产品类型添加到列表中。

var types = (from t in NHibernateSession.Linq<Product>()
select t.ProductType).Distinct().ToList<ProductType>();
return types;

但是它给出了一个无法转换类型错误的对象

“...Domain.Product”以键入“...Domain.ProductType”。

ProductType 是 Product 的一个属性。

<many-to-one name="ProductType" class="Portal.Domain.ProductType, TilePortal.Domain" column="ProductTypeID" not-null="true" ></many-to-one>

编辑:看起来 Linq to Nhibernate 还不够成熟,无法处理此类查询。我只是希望能够创建一个简单的 SQL 查询,从数据库中获取不同的 ProductType,而无需带来所有产品,这对于拥有数百万产品的生产数据库来说是行不通的。因此,如果您可以说明如何使用 HQL os Criteria API 来执行此操作,那也可以..

最佳答案

您可能遇到了 nHibernate LINQ 实现的问题。你有没有得到同样的错误:

 var types = (from t in NHibernateSession.Linq<Product>()
select t.Type);

如果不是,当您尝试使用非泛型 ToList() 时会发生什么?

 var types = (from t in NHibernateSession.Linq<Product>()
select t.Type).Distinct().ToList();

顺便说一句,我通常会避免创建一个名称与 .NET 框架中的某些内容冲突的类,尤其是像 Type 这样的基本类。这只会导致困惑。或许您应该将其重命名为 ProductType。

关于c# - LINQ 的转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2629334/

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