C# 无法转换类型“AnonymousType#1”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11827870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot convert type 'AnonymousType#1'
提问by RPD
Do you know how fix this error? It` s show error in this line "foreach (int s in itemColl)"
你知道如何解决这个错误吗?在“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
错误 1 无法将类型 'AnonymousType#1' 转换为 '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);
}
采纳答案by Habib
You are selecting itemCollwith newkeyword, defining an anonymous type, you can't apply foreachloop with inttype. Your current query is returning something like IEnumerable<AnonymousType>
您正在itemColl使用new关键字进行选择,定义匿名类型,您不能foreach使用int类型应用循环。您当前的查询返回类似IEnumerable<AnonymousType>
Instead you may do:
相反,您可以这样做:
var itemColl = from p in re.Kategorie
where p.Nazwa == category
select p.Id_Kat;
This will return IEnumerable<int>and that you can use in your current foreach loop.
这将返回IEnumerable<int>,您可以在当前的 foreach 循环中使用它。
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.
但是,如果您想将当前查询与匿名类型的选择一起使用,您必须使用隐式类型var修改您的 foreach 循环,并且由于您当前的查询返回一个匿名类型对象,您可以Id_kat从该对象中选择。就像是。
foreach (var s in itemColl)
{
Console.WriteLine(s.Id_kat);
}
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 return IEnumerable<int>
IMO,不推荐第二种方法,因为您只是返回int包装在匿名类型中的类型。如果您可以更改查询以返回,那就更好了IEnumerable<int>
回答by SeToY
Well, you could return a real (int)-value instead of an anonymous linq-result
好吧,你可以返回一个真正的 (int)-value 而不是匿名的 linq-result
var itemColl = from p in re.Kategorie
where p.Nazwa == category
select p.Id_Kat;
回答by DaveShaw
You just need to change the select to:
您只需要将选择更改为:
var itemColl = from p in re.Kategorie
where p.Nazwa == category
select p.Id_kat;
This will create an IEnumerable<int>which is what you are trying to use in the foreach.
这将创建一个IEnumerable<int>您要在foreach.
The select new { p.Id_Kat }is creating a new Anonymous Typewhich is in the simplest way of saying it is a new class like this:
该select new { p.Id_Kat }是创建一个新的匿名类型这是在说,它是这样的一类新的简单的方法:
class AnonymousType#1
{
public int Id_Kat {get; set;}
}
回答by Pravin Pawar
var itemColl = from p in re.Kategorie
where p.Nazwa == category
//This is Anonymous Type
select new
{
//Anonymous type's attribute(s) go(es) here
IntItem = p.Id_kat
};
foreach (var s in itemColl)
{
Console.WriteLine(s.IntItem);
}

