views:

161

answers:

2

I am trying to get the rest of the select keys in this linq query but the intellisense is giving me an error

var query2 = from row2 in query1
                     group row2 by row2.questionid into g
                     where g.Count() > 0
                     select new
                     {
                         questionid1,   //Error here
                         time,          //Error here
                         thecount = g.Count()
                     };

How do I get those select keys?

+3  A: 

I'm assuming that questionid and time are the properties you want to group on:

You can only get the keys you grouped on from g, and as Jon has suggested the where clause doesn't actually do anything.

Try this:

var query2 = from row2 in query1
             group row2 by new { row2.questionid, row2.time } into g
//           where g.Count() > 0
             select new
             {
                 g.Key.questionid,
                 g.Key.time,
                 thecount = g.Count()
             };
Yannick M.
A: 

It's not at all clear what you're asking, I'm afraid. What is "time"? What's the difference between questionid and questionid1?

By the time you've grouped, you've basically got a sequence of groups rather than a sequence of questions. When you project those groups, you can use the Key property of the group to get at the key which forms that group, and you can use the sequence of values within the group (which is what g.Count() is doing in your example). For example, you could take the first result within the group, and access individual fields within that.

If you could give more of an idea of what data you've got and what you're trying to achieve, we're much more likely to be able to help you.

As an additional note - your where clause isn't doing anything at the moment. When you group by a key, you never get any "empty" groups - LINQ isn't going to make up keys which aren't in the actual data.

Jon Skeet