Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?)

like image 888
RE0824C Avatar asked Jan 09 '23 08:01

RE0824C


1 Answers

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()
like image 171
MarcinJuraszek Avatar answered Jan 28 '23 06:01

MarcinJuraszek