我正在使用EntitFramework来生成我的实体类:
我有这些课程:
public class Car
{
//...
public String Brand { get; set; }
//...
public virtual ICollection<CarLocalized> CarLocalizeds { get; set; }
//...
}
public class CarLocalized :ILocalized
{
public int LangID { get; set; }
public Lang Lang { get; set; }
}
public static class Helper {
public static List<String> GetLangIDList(ICollection<ILocalized> list)
{
//I want all the ID of the lang where car is translated for:
var somethin = list.Select(m => m.LCID_SpracheID.ToString()).ToList();
return somethin;
}
}
public class HomeController : Controller
{
public ActionResult Translated()
{
Car car = db.Cars.Find(2);
List<String> transletedIDs = Helper.GetLangIDList(car.CarLocalizeds);
return View(transletedIDs);
}
}
但现在问题在于
List<String> transletedIDs = Helper.GetLangIDList(car.CarLocalizeds);
无效。为什么我不能将签名设置为ICollection并为其提供ICollection,其中CarLocalized实现Signature中所需的接口?
请帮帮我......
THx的
答案 0 :(得分:1)
问题是ICollection<T>
不是“协变”。似乎GetLangIDList
方法不修改列表,它只是查询列表。在这种情况下,您可以使用“{变形”的IEnumerable<T>
。
public static List<String> GetLangIDList(IEnumerable<ILocalized> list)
{
//I want all the ID of the lang where car is translated for:
var somethin = list.Select(m => m.LCID_SpracheID.ToString()).ToList();
return somethin;
}