2013-11-27 67 views
2

我有以下模型类: -无法隐式转换类型“System.Linq.IQueryable <TMS.Models.CustomAsset>”到“了System.Collections.Generic.ICollection

public class CustomerCustomAssetJoin 
{ 
    public CustomAsset CustomAsset { get; set; } 
    public ICollection<CustomAsset> CustomAssets { get; set; } 

} 

但是,当我写的下面的方法: -

public CustomerCustomAssetJoin CustomerCustomAsset(string customerName) 
{ 
    var customerAssets = tms.CustomAssets.Include(a => a.CustomAssetType).Where(a => a.CustomerName.ToLower() == customerName.ToLower()); 
    CustomerCustomAssetJoin caj = new CustomerCustomAssetJoin 
    { 
     CustomAsset = new CustomAsset {CustomerName = customerName }, 
     CustomAssets = customerAssets 
    }; 
    return caj; 
} 

我有以下异常:

错误20无法隐式转换类型 'System.Linq.IQueryable'到 'System.Collections.Generic.ICollection'。一个 显式转换存在(您是否缺少演员?)

那么是什么原因导致了这个错误?

var customerAssets = tms.CustomAssets.Include(a => a.CustomAssetType).Where(a => a.CustomerName.ToLower() == customerName.ToLower()); 

那么,为什么我必须将它转化成一个列表:要如下克服这个错误我刚加入.toList()?

+0

ICollection和IQueryable是不同的东西。 http://stackoverflow.com/questions/4455428/difference-between-iqueryable-icollection-ilist-idictionary-interface –

回答

2

这是因为LINQ Include方法返回ObjectQuery(T)类型的对象,而你的类期待它实现了ICollection<T>接口的对象,其实现了IQueryable<T>接口。由于这两个对象不能从一个隐式转换到另一个,所以必须明确地将Include方法的结果转换为List类型,该类型实现了ICollection<T>接口。

4

你存储在customerAssets中的内容只是一个查询 - 一种方式,如何获取数据。这不是数据本身,因为它被懒惰地评估。 ICollection<T>是为处理已有的数据集合而构建的界面。该查询没有实现它,所以你不能隐式地从IQueryable<T>转换到ICollection<T>调用ToList()是一种简单的方法,如何强制将数据加载到ICollection<T>,但它也意味着在你的情况下,在该地方的代码(和执行时间),查询将被执行,数据将从您正在查询的任何数据库中加载。

3

这是因为您将CustomAssets定义为ICollection<CustomAsset>,而IQueryable<T>未执行ICollection<T>。你需要一个执行ICollection<CustomAsset>的结果,当你申请ToList()时,它会转换成List<CustomAsset>这实际上实现了ICollection<CustomAsset>

+0

所以应用.Tolist()是正确的解决方案? –

+0

@JohnJohn是的,除非您将'CustomAssets'重新定义为'IQueryable ',否则使用'ToList()'当然会运行查询权并为您获取项目。 –

相关问题