我有一个带有接口(interface)的实体:
public interface IStore
{
int Id { get; }
string Name { get; set; }
}
public class Store : IStore
{
public Store(int d)
{
}
public virtual int Id { get; protected set; }
public virtual string Name { get; set; }
public virtual IList<Product> Products { get; set; }
public virtual IList<Employee> Staff { get; set; }
public Store()
{
Products = new List<Product>();
Staff = new List<Employee>();
}
public virtual void AddProduct(Product product)
{
product.StoresStockedIn.Add(this);
Products.Add(product);
}
public virtual void AddEmployee(Employee employee)
{
employee.Store = this;
Staff.Add(employee);
}
}
我有一个特定的 Linq 方法,返回一个可查询的接口(interface):
private static IQueryable<Store> GetStores(ISession session)
{
return session.Query<Store>();
}
但是 Single() 和 First() 方法会抛出异常。
var stores = GetStores(session);
var s = stores.First(e => e.Id == 37); // crash
var s1 = stores.Single(e => e.Id == 37); // crash
产量:
'System.Linq.EnumerableQuery`1[TestFluentNhibernate.IStore]' cannot be converted to System.Linq.IQueryable`1[TestFluentNhibernate.Store]
Where().ToList()
工作正常...
为什么?
最佳答案
参见 here .
var s = stores.Where(e => e.Id == 37).ToList().First();
var s1 = stores.Where(e => e.Id == 37).ToList().Single();
可能会解决问题。
关于c# - Linq 到 NHibernate : 'System.Linq.EnumerableQuery` 1[Entity]' cannot be converted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28318458/