Reputation: 9969
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?
Upvotes: 1
Views: 2590
Reputation: 3256
In the Constructor<T>()
method you expect a List<T>
type but you provide an instance of IEnumerable<T>
.
IEnumerable<T>
List<T>
typeIEnumerable<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
}
Upvotes: 2
Reputation: 101573
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());
Upvotes: 1