RE0824C RE0824C - 9 days ago 5
C# Question

Cannot implicitly convert type 'System.Collections.Generic.List<String>' to 'System.Collections.Generic.IEnumerable<turon.Model.Products_Products>

public IEnumerable<Products_Products> getprod()
{
var db = new Model.atengturonDBEntities();
IEnumerable<Products_Products> x = new List<Products_Products>();
var test = (from name in db.Products_Products select
name.ProductName).ToList();
x = test;
return x;
}


why am I getting this error? I also tried to change all 'IEnumerable' to 'List', Help me! thanks :)


Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exist(are you missing a cast?)

Answer

Error message is quite clear. Your LINQ query returns collection of string, because you're selecting name.ProductName, but your method is declared to return collection of Products_Products.

Change your LINQ query to return not just ProductName but the product itself:

IEnumerable<Products_Products> x = new List<Products_Products>();
var test = (from name in db.Products_Products select 
                name).ToList();

(can also be replaced with just db.Products_Products.ToList().

Or change your method to return IEnumerable<string>

public IEnumerable<string> getprod()
Comments