在做这个的时候
IEnumerable<Colors> c = db.Products.Where(t => t.ProductID == p.ProductID).SelectMany(s => s.Colors);
if (c.Any()) sp.color = Constructor(c);
以后
private string Constructor<T>(List<T> list)
{
//Do something
}
我得到了错误
The type arguments for method 'Controller.Constructor(System.Collections.Generic.List)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
当然是不对的。但是我错过了什么?
最佳答案
在Constructor<T>()
您期望的方法 List<T>
类型,但您提供了 IEnumerable<T>
的实例.
- 将方法参数类型更改为
IEnumerable<T>
- 将查询转换为
List<T>
类型
IEnumerable<Colors> c =
db
.Products
.Where(t => t.ProductID == p.ProductID)
.SelectMany(s => s.Colors)
.ToList();
if (c.Any()) sp.color = Constructor(c);
private string Constructor<T>(IEnumerable<T> list)
{
//Do something
}
关于c# - 无法从用法中推断出方法 '' 的类型参数。尝试明确指定类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36829132/