Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 方法'';无法从用法推断。尝试显式指定类型参数_C#_Linq - Fatal编程技术网

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());