我有以下模型类: -无法隐式转换类型“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()?
ICollection和IQueryable是不同的东西。 http://stackoverflow.com/questions/4455428/difference-between-iqueryable-icollection-ilist-idictionary-interface –