Reputation: 78
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.
Upvotes: 4
Views: 5319
Reputation: 10418
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).
Upvotes: 0
Reputation: 117350
Additional
Perhaps you want to use this outside the method? Then use something like this:
public void ForEach<T>(IEnumerable<T> l, Action<T> a)
{
foreach (var e in l) a(e);
}
Usage:
ForEach(from x in bar select new { Foo = x, Frequency = 4444, Pitch = 100 },
x =>
{
//do stuff here
Console.WriteLine(x.Foo);
Console.Beep(x.Pitch,x.Frequency);
});
Upvotes: 2
Reputation: 107626
You can treat the result as you would a regular C# object. Intellisense will help you out with the anonymous typing.
foreach (var anonymousObject in q)
{
// anonymousObject.CountryID;
// anonymousObject.count;
}
Upvotes: 1
Reputation: 437854
Just like as if it were any other kind of object:
foreach(var obj in q) {
var id = obj.CountryID;
var count = obj.count;
}
Upvotes: 6