简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM