C# 方法'';无法从用法推断。尝试显式指定类型参数
在做这个的时候C# 方法'';无法从用法推断。尝试显式指定类型参数,c#,linq,C#,Linq,在做这个的时候 IEnumerable<Colors> c = db.Products.Where(t => t.ProductID == p.ProductID).SelectMany(s => s.Colors); if (c.Any()) sp.color = Constructor(c); IEnumerable c=db.Products.Where(t=>t.ProductID==p.ProductID)。选择多个(s=>s.Colors); 如果(c.An
IEnumerable<Colors> c = db.Products.Where(t => t.ProductID == p.ProductID).SelectMany(s => s.Colors);
if (c.Any()) sp.color = Constructor(c);
IEnumerable c=db.Products.Where(t=>t.ProductID==p.ProductID)。选择多个(s=>s.Colors);
如果(c.Any())sp.color=构造函数(c);
后来
private string Constructor<T>(List<T> list)
{
//Do something
}
私有字符串构造函数(列表)
{
//做点什么
}
我得到了错误
方法的类型参数
“Controller.Constructor(System.Collections.Generic.List)”不能
可以从用法推断。尝试指定类型参数
明确地说
当然这是不对的。但我缺少什么呢?构造函数需要具体的类型(
List
),然后将其传递给接口(IEnumerable
)。想象一下,在IEnumerable
下有类似于ReadOnlyCollection
的内容-您将如何将其转换为List?你不能。因此,如果在构造函数中未使用任何特定于列表的内容,请将签名更改为:
private string Constructor<T>(IEnumerable<T> list)
{
//Do something
}
在Constructor()
方法中,您需要一个列表
类型,但您提供了一个IEnumerable
的实例
- 将方法参数类型更改为
IEnumerable
- 将查询转换为
类型列表
IEnumerable c=
分贝
.产品
.其中(t=>t.ProductID==p.ProductID)
.选择多个(s=>s.颜色)
.ToList();
如果(c.Any())sp.color=构造函数(c);
私有字符串构造函数(IEnumerable列表)
{
//做点什么
}
if (c.Any()) sp.color = Constructor(c.ToList());