score:3
The other answers are ordering the initial list and then ordering the groups. This works because GroupBy preserves the order of the initial collection. However, if you want to actually order the groups (which was the original question), you want to create the groups first, and then order by the Key of the IGrouping object:
[Test]
public void Test()
{
var collection = new List<Fake>()
{
new Fake {Value = "c"},
new Fake {Value = "b"},
new Fake {Value = "a"},
new Fake {Value = "a"}
};
var result =
collection.GroupBy(a => a.Value, a => a).OrderBy(b => b.Key).ToList();
foreach(var group in result)
{
Console.WriteLine(group.Key);
}
}
private class Fake
{
public string Value { get; set; }
}
Using query syntax, this looks like this:
var result =
from a in collection
group a by a.Value
into b
orderby b.Key
select b;
Again, we're doing the group by operation before the order by operation, and actually performing the ordering on the group, not on the original list elements.
score:0
Try:
var result = from a in DB.Table
orderby a.Value
group a by a.Value into b
select b;
EDIT:
Some example code using the above query:
static void Main(string[] args)
{
var s = new List<Animal>
{
new Animal {Value = "Zebra"},
new Animal {Value = "Zebra"},
new Animal {Value = "Cobra"},
new Animal {Value = "Apple"}
};
var result = from a in s
orderby a.Value
group a by a.Value into b
select b;
foreach (var group in result)
{
foreach (var animal in group)
{
Console.WriteLine(animal.Value);
}
}
}
class Animal
{
public string Value { get; set;}
}
Source: stackoverflow.com
Related Articles
- Numbering the rows per group after an orderby in LINQ
- Access all of the data after joining two tables and group them using linq
- Why is my code doing lazy loading even after I turned it off at every possible point?
- Linq OrderBy to group objects with the same sets
- Left join after a into group in Linq using entity framework (core)
- LINQ Source Code Available
- multiple orderby in this linq code
- .NET 4 Code Contracts: "requires unproven: source != null"
- orderby certain property - code improvement
- how to select top 1 from each group after order by
- why Linq GroupBy After OrderBy dismissed order operation?
- LINQ Lambda efficiency of code groupby orderby
- Entity framework 5.0 First or Group By Issue- After upgrading from 2.2 to 5.0
- EF Code first Eager loading and OrderBy problem
- LINQ and EntityFramework: Getting first result after group by
- creating Linq to sqlite dbml from DbLinq source code
- Is it possible to OrderBy a List after a certain element?
- Return List<string> after group by
- Linq for calculating rank with group and orderby
- Using an expression tree to generate a select statement with a sum after a group by C#
- how one can calculate a weight average price after a group by?
- OrderBy After Group?
- How to Select top (5) contributors group by Business type code in c# linq query
- Get Sum of Columns after its group using linq
- How do I to use Where, Group By, Select and OrderBy at same query linq?
- EF core 3 nested group by after restricting client evaluation
- Linq OrderBy Attribute Value and Group
- Accesing object properties held in List<T> after LINQ group by procedure
- source code for LINQ 101 samples
- LiNQ using ThenBy after OrderBy
- How to access content of the groups shaped by LINQ GroupBy?
- Linq To SQL caching VS multi-user application
- SingleOrDefault in generic repository?
- How can I return in a ObsevableCollection<T> using C# Linq, all items if no one is selected or just selected items if at least one is selected?
- Binding Linq.EntityRef to Gridview
- the best way to convert delimited to fixed width
- How to stop a GraphicsPath from closing
- Linq Conditional .Any() Select
- How can I concatenate all the column values in a row, and then concatenate all rows in a DataTable into a single string?
- UPDATE SELECT in LINQ to SQL
- linq calculated value with group by
- Linq sort first two numbers in long type
- Group linq results by value and group null or invalid values and do counts
- Getting this exception: "DbSortClause expressions must have a type that is order comparable. Parameter name: key"
- Find third Max salary using Linq
- Find object that meets multiple conditions using LINQ
- Cannot access Linq when using Razor without MVC
- Enumerable.Range in and Expression and Entity Framework
- Linq to Entities or SQL Server? Cannot get the results i require. MVC EF
- Linq Lambdas vs SQL Like Notation