Marvin
Marvin

Reputation: 569

Group method not recognized in LINQ query

LINQ-newb having trouble using grouping. I'm trying to query an IEnumerable called dosesWithHighestScore.

I'm following this example: http://msdn.microsoft.com/en-us/vcsharp/aa336747.aspx#minGrouped

Here's my code:

    var doseWithLowestVolumeForForm =
        from d in dosesWithHighestScore
        group d by d.formulation.form into g                
        select new { 
            Form = g.Key, 
            LowDose = g.Group.Min(d => d.doseAdministrableAmount)
        };

The use of "Group" is causing this error:

'System.Linq.IGrouping' does not contain a definition for 'Group' and no extension method 'Group' accepting a first argument of type 'System.Linq.IGrouping' could be found (are you missing a using directive or an assembly reference?)

Here are my usings:

using System;
using System.Text;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Collections;

Why am I getting this error?

Upvotes: 0

Views: 1981

Answers (1)

LBushkin
LBushkin

Reputation: 131806

You an get rid of the Group term in your assignment to LowDose. In LINQ-to-objects, a group is itself an IEnumerable<>, so you can apply other LINQ operations to it without any additional qualifiers. The Key property is provided on IGrouping so that you can access the value by which the group was ... well, grouped.

var doseWithLowestVolumeForForm =
        from d in dosesWithHighestScore
        group d by d.formulation.form into g                
        select new { 
            Form = g.Key, 
            LowDose = g.Min(d => d.doseAdministrableAmount)
        };

The examples on the site that show the use of a Group property are incorrect.

Upvotes: 3

Related Questions