迭代 LINQ AnonymousType 对象
如何在另一个方法中使用此 LINQ 的结果并获取属性 CountryID
和 count
?
public IQueryable GetRequestsByRegion(string RequestType)
{
try
{
var q = from re in context.RequestExtensions
from r in context.Requests
where re.ExtensionID == r.ExtraInfoID
&& r.OriginalRequestID == null
&& r.RequestType == RequestType
group re by new { CountryID = re.CountryID } into grouped
select new { CountryID = (int)grouped.Key.CountryID, count = (int)grouped.Count(t => t.CountryID != null) } ;
return q;
}
catch (Exception ex)
{
}
return null;
}
public void GetAllInformationRequestsByRegion()
{
IQueryable dict = GetRequestsByRegion("tab8elem1");
/* How to iterate and get the properties here? */
}
返回类型和变量类型不需要是指示的类型......这只是我的尝试。我也在使用 WCF,因此无法返回对象类型。
How to use the result of this LINQ in another method and get the properties CountryID
and count
?
public IQueryable GetRequestsByRegion(string RequestType)
{
try
{
var q = from re in context.RequestExtensions
from r in context.Requests
where re.ExtensionID == r.ExtraInfoID
&& r.OriginalRequestID == null
&& r.RequestType == RequestType
group re by new { CountryID = re.CountryID } into grouped
select new { CountryID = (int)grouped.Key.CountryID, count = (int)grouped.Count(t => t.CountryID != null) } ;
return q;
}
catch (Exception ex)
{
}
return null;
}
public void GetAllInformationRequestsByRegion()
{
IQueryable dict = GetRequestsByRegion("tab8elem1");
/* How to iterate and get the properties here? */
}
The return types and variable types don't need to be the ones indicated... This was just my try. I am also using WCF so I can't return Object types.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
就像它是任何其他类型的对象一样:
Just like as if it were any other kind of object:
附加
也许您想在方法之外使用它?然后使用类似这样的东西:
用法:
Additional
Perhaps you want to use this outside the method? Then use something like this:
Usage:
您可以像对待常规 C# 对象一样对待结果。 Intellisense 将帮助您完成匿名输入。
You can treat the result as you would a regular C# object. Intellisense will help you out with the anonymous typing.
匿名类型对于声明它们的方法来说是本地的。您不能直接返回它们。如果您需要将它们公开在声明方法之外,则需要投影到您可以命名的类型(您自己的自定义类或其他现有框架类,如 KeyValuePair)。
Anonymous types are local to the method that they are declared in. You can't return them directly. If you need them to be exposed outside of the declaring method, you need to project into a type that you can name (either your own custom class or some other existing framework class like KeyValuePair).