c# – 遍历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,所以我不能返回对象类型.

最佳答案
就像它是任何其他类型的对象一样:

foreach(var obj in q) {
    var id = obj.CountryID;
    var count = obj.count;
}

转载注明原文:c# – 遍历LINQ AnonymousType对象 - 代码日志