LINQ: No overload for method 'GroupBy' takes 6 arguments / IGrouping<t,t> does not contain a definition

444 Views Asked by At

Problem: Getting error

"No overload for method 'GroupBy' takes 6 arguments"

A lot of the SO articles are for specific user created methods. GroupBy is part of the library.

I've tried varying the number of arguments. If I change it to 2 arguments, then the errors points to the next area where it has OrderByDescending and gives the error:

"IGrouping does not contain a definition for 'Start_Date' and no extension method 'Start_Date' accepting a first argument of type 'IGrouping' could be found."

The code that gives this error is:

var someVariable = DbContextObject.View.Where(
                        m =>
                            m.Some_ID == CurrentlyEditingSomeID
                        ).GroupBy(d=> d.Start_Date,f=>f.Some_ID).AsQueryable().OrderByDescending(m => m.Start_Date);

To be used in a ListView

3

There are 3 best solutions below

0
On

You need to create anonymous object with list of all fields to be included in group by, then access those fields using Key property of grouped list, something like below -

var someVariable = DbContextObject.View.Where(
                        m =>
                            m.Some_ID == CurrentlyEditingSomeID
                        ).GroupBy(d=> new { d.Start_Date,d.Some_ID}).AsQueryable().OrderByDescending(m => m.Key.Start_Date);
5
On

Update:

You have simple problem in you code, change you code to this:

var rooms = roomBinding.GroupBy(g => new { Id = g.R_ID, Name = g.r_name })
                   .Select(g => new
                       {
                           Id = g.Key.Id,
                           Name = g.Key.Name,
                           Count = g.Count()
                       });

After new in grouping you have to new and set your data like my sample.

0
On

So the input of your GroupBy is a sequence of Views. Every View has at least a StartDate and SomeId.

Your GroupBy groups all input Views into groups of of items extracted from the Views with the same StartDate. Every Group has a Key containing this common StartDate, the elements in the group are the SomeId of the Views in the group.

The result of the GroupBy is already IQueryable<...>. So AsQueryable is unnecesary, it will only slow down your process.

The input of your Orderby is a sequence of groups. Alas, groups don't have a StartDate. Luckily, groups have a Key containing the StartDate that you want to order by.

var result = DbContextObject.Views
    // I onlly want the views with a certain SomeId:
    .Where(view => view.SomeID == CurrentlyEditingSomeID)

    // group the views into groups with same StartDate
    // the elements in the group are the SomeId
    .GroupBy(view => view.StartDate, view=>view.SomeID)
    // result: a sequence of Groups with a Key and a sequence of SomeId objects

    // order the Groups by StartDate, This StartDate is in the Key
    .OrderBy(group => group.Key);

By the way, if you don't want a Key, and insist on having a StartDate, there is a less known overload of GroupBy. A version where you can Select what you want in your output.

.GroupBy(view = view.StartDate,         // make groups with same StartDate
    view => view.SomeId,                // select SomeId as elements in the group
    (commonStartDate, someIds) => new   // from every commonStartDate and the someIds
    {                                   // in this group make a new object
        StartDate = commonstartDate,    // containing this common StartDate
        SomeIds = someIds,              // and the SomeId objects in the group
    })
    .OrderBy(group => group.StartDate); // now you can order by StartDate instead of Key