【发布时间】:2013-12-13 01:20:39
【问题描述】:
我有以下模型类:-
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'。一个 存在显式转换(您是否缺少演员表?)
那么是什么导致了这个错误?为了克服这个错误,我只需添加一个 .toList() 如下:
var customerAssets = tms.CustomAssets.Include(a => a.CustomAssetType).Where(a => a.CustomerName.ToLower() == customerName.ToLower());
那我为什么要把它转换成一个列表呢?
【问题讨论】:
-
ICollection 和 IQueryable 是不同的东西。 stackoverflow.com/questions/4455428/…
标签: c# asp.net-mvc asp.net-mvc-4