1
votes

While doing this one

IEnumerable<Colors> c = db.Products.Where(t => t.ProductID == p.ProductID).SelectMany(s => s.Colors);
if (c.Any()) sp.color = Constructor(c);

and later on

private string Constructor<T>(List<T> list)
{
     //Do something
}

i get the error

The type arguments for method 'Controller.Constructor(System.Collections.Generic.List)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Of course it is not correct. But what am i missing?

2

2 Answers

2
votes

In the Constructor<T>() method you expect a List<T> type but you provide an instance of IEnumerable<T>.

  • change the method parameter type to IEnumerable<T>
  • convert to query to List<T> type
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
}
1
votes

Constructor expects concrete type (List<T>) and you pass it interface (IEnumerable<T>). Imagine that under IEnumerable<T> there is something like ReadOnlyCollection<T> - how would you cast that to List? You cannot. So if you don't use anything List-specific in your Constructor, change signature to:

private string Constructor<T>(IEnumerable<T> list)
{
 //Do something
}

Otherwise - convert your Colors to list via .ToList() extension method:

if (c.Any()) sp.color = Constructor(c.ToList());