Do you know how fix this error? It` s show error in this line “foreach (int s in itemColl)”
What I have to do?
Error 1 Cannot convert type ‘AnonymousType#1’ to
‘int’ C:\Users\Rafal\Desktop\MVC ksiązka\moj
projekt\sklep\SportsStore.WebUI\Controllers\ProductController.cs 37 21 SportsStore.WebUI
var itemColl = from p in re.Kategorie
where p.Nazwa == category
select new
{
p.Id_kat
};
foreach (int s in itemColl)
{
Console.WriteLine(s);
}
You are selecting
itemCollwithnewkeyword, defining an anonymous type, you can’t applyforeachloop withinttype. Your current query is returning something likeIEnumerable<AnonymousType>Instead you may do:
This will return
IEnumerable<int>and that you can use in your current foreach loop.But if you want to use your current query with selection on anonymous type, you have to modify your foreach loop with implicit type var, and since your current query is returning an anonymous type object you may select the
Id_katfrom the object. Something like.IMO, the second approach is not recommended because you are just returning an
inttype wrapped inside an anonymous type. Its better if you can change your query to returnIEnumerable<int>