0

I have Rows object that is IEnumerable<dynamic>, it has 5 properties (columns) and 100 rows. One of the properties/columns is Group, only 2 distinct groups out of 100 rows, so first I run a distinct against it:

IEnumerable<dynamic> Groups = Rows.Select(x => x.Group).Distinct();

This works, no error.

Then I want to go back to my Rows object and loop through them where this group = the group in Rows, like this:

foreach (string Group in Groups)
{
    IEnumerable<dynamic> GroupData = 
       from rowdata in Rows
       where rowdata.Group = @Group 
       select rowdata;

But I get this error on the last line:

'WebMatrix.Data.DynamicRecord' does not contain a definition for 'Group'

Anyone knows why this isn't working?

Surely I can do this another way, but I wanted to use c# select statement instead. How can I though?


Edit to show usage:

    foreach (var row in GroupData){
       string ThisGroup = row.Group
    }
     ...
5
  • what is @Group do you mean to say where rowdate.Group = "Group" Commented Sep 12, 2014 at 16:19
  • 3
    1) = is assignment, not comparison 2) The equivalent of Rows.Select(x => x.Group) would be from x in Rows select x.Group. Commented Sep 12, 2014 at 16:20
  • Can you show the code where you then use GroupData ? Commented Sep 12, 2014 at 16:21
  • @DJ KRAZE @Group is Group in the foreach statement. I put a @ because it's a reserved name in select statements it appears? Commented Sep 12, 2014 at 16:28
  • @p.s.w.g So you mean i should do where rowdata.Group == @Group ? and first select works, it's the 2nd one giving the error. Commented Sep 12, 2014 at 16:48

1 Answer 1

3

Instead of selecting twice, group on the Group value:

IEnumerable<IGrouping<string, dynamic>> groups = Rows.GroupBy(x => (string)x.Group);

Now you can just loop through the result:

foreach (IGrouping<string, dynamic> group in groups) {
  ...
}

The IGrouping<> object has a Key property which is the value that you grouped on, and it's also a collection of the values in the group.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.