c# - Linq 到 NHibernate : 'System.Linq.EnumerableQuery` 1[Entity]' cannot be converted

标签 c# linq nhibernate

我有一个带有接口(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/

相关文章:

c# - 是否有必要在交易中包装 nHibernate future ?

c# - '字典中不存在给定的键' - 但键存在

c# - WPF 和 MVVM - 太多了?

nhibernate - 如何将对象从反射转换为通用集合?

nhibernate - 我应该将 nhibernate 用于一个相当小的项目吗?

C#/LINQ 比较两个列表和赋值的最快方法

c# - OWIN Startup 方法如何获取网站的基本 URL?

c# - 如何从强制终止管道中正确清除异步回调?

sql - 将 LINQ to SQL 与动态表一起使用

C# linq to json 如何读取数组中的属性值