-1

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

seesharp
  • 101
  • 1
  • 14
  • Sounds to me like you've got a parenthesis in the wrong spot. I don't see any calls with six arguments in your example. – John Wu Oct 03 '18 at 22:09
  • The original had 6, the updated had 2 which changed the error. The original was: GroupBy(a=>a.G_Description, b=>b.G_ID, c=>c.Plan, d=> d.Start_Date, e=> e.End_Date, f=>f.Some_ID) – seesharp Oct 03 '18 at 22:12

3 Answers3

1

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);
Vinit
  • 2,540
  • 1
  • 15
  • 22
1

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
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
0

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.

user229044
  • 232,980
  • 40
  • 330
  • 338
AmirReza-Farahlagha
  • 1,204
  • 14
  • 26
  • Hi. I tried this but at the OrderByDescending, I get an error on Start_Date. The error is: "IGrouping<, View> does not contain a definition for Start_Date – seesharp Oct 04 '18 at 13:17
  • Thanks. Trying this I get error "Cannot implicitly convert type 'System.Linq.IQueryable<>' to 'System.Linq.IQueryable' ///// method signature is public IQueryable – seesharp Oct 04 '18 at 13:54
  • 1
    @seesharp Remove AsQueryable method, because you query is queryable result. – AmirReza-Farahlagha Oct 04 '18 at 14:18
  • What would the return type be then? – seesharp Oct 04 '18 at 14:24
  • 1
    @seesharp Do you know when EF return data?? When you use the methods like FirstOrDefault(), First(), SingleOrDefault(), Single(), ToList(), ToListAsync() or some method like these. Now If you don’t use of those methods, you result is IQueryable. Now you return type is IQueryable. – AmirReza-Farahlagha Oct 04 '18 at 14:29