Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate through LINQ AnonymousType object

Tags:

c#

linq

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.

like image 469
Luís Sousa Avatar asked Jan 18 '23 10:01

Luís Sousa


1 Answers

Just like as if it were any other kind of object:

foreach(var obj in q) {
    var id = obj.CountryID;
    var count = obj.count;
}
like image 80
Jon Avatar answered Jan 30 '23 14:01

Jon