我有一个带接口的实体:
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的特定方法,返回一个可查询的接口:
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()
确实可以正常工作......
为什么?