Andrew Stopford's Weblog

poobah

News

Articles

Family

MbUnit Folks

Old Blogs

Using Where and Group in Linq

I tried lots of Google time to find the answer to this and failed (my google searching may also suck :) so in trying to tie a Where and GroupBy together in a Linq query I used the following.

var values = from p in tbl_data                 
              where
              p.some_other_value == 'hello world'
             select p;

var group_values = values.GroupBy(p => p.a_value).ToList();

I've seen lots of queries that place the groupby in the from and but never with a Where, there may be a nicer way of doing it but the above works for me :)

Posted: May 16 2008, 12:53 PM by andrewstopford | with 7 comment(s)
Filed under: ,

Comments

FransBouma said:

why doesn't

var values = from p in tbl_data                

             where

             p.some_other_value == 'hello world'

            group p by p.a_value into g

            select g;

work for you?

# May 16, 2008 9:07 AM

andrewstopford said:

Thanks, Frans that works too. My version used Generic Lists of IGrouping and your version IQueryable which is nicer. I figured there would be a nicer way of doing this but a) I was being stupid and b) crap at searching google.

# May 16, 2008 10:29 AM

Fabrice said:

Hum... I don't understand what you're trying to achieve and what the problem is.

# May 16, 2008 12:28 PM

Dew Drop - May 17, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - May 17, 2008 | Alvin Ashcraft's Morning Dew

# May 17, 2008 10:08 PM

Weekly Link Post 42 « Rhonda Tipton’s WebLog said:

Pingback from  Weekly Link Post 42 « Rhonda Tipton’s WebLog

# May 18, 2008 8:09 PM

Dampee said:

why not using the dot-notation?

var values = tbl_data.where(p =>           p.some_other_value == 'hello world').GroupBy(p => p.a_value).Select(p => p);

# July 2, 2008 4:51 PM

Smarter Than You said:

Because dot notation sucks... duh

# September 24, 2008 12:26 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)