Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 林克对NHibernate:&x27;System.Linq.EnumerableQuery`1[实体]和#x27;无法转换_C#_Linq_Nhibernate - Fatal编程技术网

C# 林克对NHibernate:&x27;System.Linq.EnumerableQuery`1[实体]和#x27;无法转换

C# 林克对NHibernate:&x27;System.Linq.EnumerableQuery`1[实体]和#x27;无法转换,c#,linq,nhibernate,C#,Linq,Nhibernate,我有一个具有接口的实体: 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; se

我有一个具有接口的实体:

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);
    }
}
收益率:

'System.Linq.EnumerableQuery`1[TestFluentNhibernate.IStore]' cannot be converted to System.Linq.IQueryable`1[TestFluentNhibernate.Store]
Where().ToList()
工作正常

为什么?

请参阅


可能会解决问题。

您所说的“不工作”是什么意思?a在这条命令中有一个例外您声明了接口的可查询性(最初是“接口集合”),但您的代码显示了其他情况,改为类的可查询性。你是映射了接口,还是只映射了类?请更好地提供。我已经映射了类,但是作为接口的可查询项返回它(它可以工作)。在将NH从3.x升级到4.0后,我遇到了一个问题。如果我查询并将对象强制转换到一个接口,并在queryfirst/Single之前使用ToList,那么它运行良好,否则会导致错误。我仍然不知道这个问题的原因。我已经这样做了(这个问题太老了),但这只是一种变通方法,不是一种正确的方法:(我记得,这只是NHYes的一个bug,ToList不是一个好的解决方案。你还记得如何解决这个问题吗,@VVildVVolf?
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]
var s = stores.Where(e => e.Id == 37).ToList().First(); 
var s1 = stores.Where(e => e.Id == 37).ToList().Single();