Timeline for answer to How to group by multiple columns using LINQ by Mo0gles
Current License: CC BY-SA 4.0
Post Revisions
15 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Oct 29, 2024 at 20:43 | comment | added | TylerH |
How are we supposed to actually write the column names in an example like this? If I try literally (e.g. for a DataTable column named Id, if I write x.Id, there is an error for that code saying "DataRow does not contain a definition for 'Id' [...]". If I put the column name in quotes, it says "Invalid anonymous type member declarator".
|
|
| May 7, 2021 at 0:32 | history | edited | John Smith | CC BY-SA 4.0 |
added 1 character in body
|
| Jun 29, 2015 at 13:40 | comment | added | Andy_Vulhop |
@AmirChatrbahr Ah, if you are returning the result of said grouping from a method, then yes, that makes sense. You can't define a method's return type as a anonymous type, so your solution works. Generally, I have pulled a group query and done some work or mapped to some model, so the use case of returning an IEnumerable<IGrouping<MyViewEntity, MyEntity>> as you did hadn't occurred to me.
|
|
| Jun 26, 2015 at 7:22 | comment | added | Amir Chatrbahr | @Andy_Vulhop let me clear it out. my problem was in DECLARING the method not calling the method and USING it. | |
| Apr 16, 2015 at 19:36 | comment | added | Andy_Vulhop |
@AmirChatrbahr It's an anonymous type, so there isn't an explicit type to define. That's why var was added to .NET. var groupedStuff = stuff.GroupBy(x => new {x.Column1, x.Column2});
|
|
| Mar 17, 2015 at 1:05 | comment | added | Amir Chatrbahr | found my answer. I need to define a new entity (MyViewEntity) containing Column1 and Column2 properties and the return type is : IEnumerable<IGrouping<MyViewEntity, MyEntity>> and Grouping code snip is : MyEntityList.GroupBy(myEntity => new MyViewEntity { Column1 = myEntity.Column1, Column2 = myEntity.Column2 }); | |
| Mar 16, 2015 at 0:30 | comment | added | Amir Chatrbahr | if i'm returning this in my method, what should be the method return type? assume my entity is 'MyEntity' | |
| Feb 24, 2015 at 15:41 | comment | added | weenoid | @Tom. I think that only applies if you're calculating a value, e.g. new { x.Column1 + " " + x.Column2 }, as there will be no name to infer. Just using the property names, as in the sample above, should always work. | |
| Jan 30, 2015 at 9:38 | comment | added | Tom Maher | @Crisfole yes I totall agree under most cases this is true. However there are times when the compiler can't infer the field names and they have to be explicitly specified. As in when you get the "Invalid anonymous type declarator" compile error. It's happened to me and also thalesfc hence the comment. | |
| Jan 28, 2015 at 13:54 | comment | added | Chris Pfohl | @Tom this should work the way it is. When you skip naming the fields of an anonymous type C# assumes you want to use the name of the finally accessed property/field from the projection. (So your example is equivalent to Mo0gles') | |
| Jul 7, 2014 at 16:51 | comment | added | tfcstack | This code is not working for me: "Invalid anonymous type declarator." | |
| Mar 1, 2013 at 14:15 | comment | added | Alex | @MGG_Soft that would be an anonymous type | |
| Feb 1, 2013 at 16:22 | comment | added | mggSoft | What type is the object returned? | |
| May 8, 2012 at 7:38 | history | edited | Pranay Rana | CC BY-SA 3.0 |
added 4 characters in body
|
| Feb 7, 2012 at 13:55 | history | answered | Mo0gles | CC BY-SA 3.0 |