Retrieving values from a Linq GroupBy?

This link on my blog should help you matlus.com/linq-group-by-finding-duplicates Essentially your type is an anonymous type so you can't reference it as a type but you can access the properties like you're trying to do I think I see your issue. If you're trying to return it from a method, you should define a type and reuturn it like shown below: public IEnumerable GetQuery() { var query = from row in stats.AsEnumerable() group row by row. Field("date") into grp select new { Date = grp.

Key, Count = grp. Count(t => t"date"! = null) }; foreach (var rw in query) { yield return new MyType(rw.

Date, rw. Count); } } declare your "query" variable using "var" as shown above.

This link on my blog should help you matlus.com/linq-group-by-finding-duplica... Essentially your type is an anonymous type so you can't reference it as a type but you can access the properties like you're trying to do. I think I see your issue. If you're trying to return it from a method, you should define a type and reuturn it like shown below: public IEnumerable GetQuery() { var query = from row in stats.AsEnumerable() group row by row.

Field("date") into grp select new { Date = grp. Key, Count = grp. Count(t => t"date"!

= null) }; foreach (var rw in query) { yield return new MyType(rw. Date, rw. Count); } } declare your "query" variable using "var" as shown above.

I am returning the query from a function and I can't return it as type var. That's why I wanted a type, unless you can suggest a way of doing this? – Bex Feb 11 at 10:26 There are other ways to do what you're trying to do (returning a linq query result) but without more details about what the calling function might possibly do with the result.

I can't suggest much, except that you should probably modify the query expression to take a delegate of some sort so the calling method can pass in a delegate in order to control what the result is. Does that make sense? – Shiv Kumar Feb 11 at 10:38 Shiv Kumar thanks for your reply.

Sorry it doesn't make too much sense! I have decided to move your example onto my page rather than keep it in the function for the time being as that works. – Bex Feb 11 at 10:40 In your foreach loop you could do a yield return of a specific type and your method signature will be such that it is an IEnumerable.

Then your caller can expect to get that type. Does that make sense? – Shiv Kumar Feb 11 at 10:43 I've modified my answer to show you another way in which you could achive this.

Essentially if define a type that has the properties you need and then in the foreach loop, you yield rreturn an instance of that type. Your method will then be returning an IEnumerable, where MyType is a the type you've defined. – Shiv Kumar Feb 11 at 10:48.

I guess you don't have access to the properties of the anonymous class because you're using IEnumerable query = .... Try var query = ... instead.

Going by your comment "I am returning the query from a function", which I take to mean that you want to do the query in a method, return the data to the caller, and then iterate the data in the caller, I suggest you return a Dictionary, like this: static Dictionary GetSummarisedData() { var results = ( from row in stats.AsEnumerable() group row by row. Field("date") into grp select new { Date = grp. Key, Count = grp.

Count(t => t"date"! = null) }) . ToDictionary(val => val.

Date, val => val. Count); return results; } then in the caller you can just foreach (var kvp in GetSummarisedData()) { // Now kvp. Key is the date // and kvp.

Value is the count }.

This sounds almost like LINQ's group by but it is a bit different, so you can't really use standard group by. However, you can write your own version (that's the wonder of LINQ!). You can either write your own extension method (e.g. GroupByMoving) or you can write extension method that changes the type from IEnumerable to some your interface and then define GroupBy for this interface.

The only thing that remains is to define AsMoving and implement GroupBy. This is a bit of work, but it is quite generally useful thing and it can be used to solve other problems too, so it may be worth doing it :-). The summary of my post is that the great thing about LINQ is that you can customize how the operators behave to get quite elegant code.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions