LINQ Multiple Group By in a List<T> then convert to List<T>
I think you're after something like this...
List<Person> persons = new List<Person>
{
new Person()
{
ID = "1",
Name = "Joe",
District = "Columbia",
Level = "10"
},
new Person()
{
ID = "2",
Name = "Beth",
District = "Columbia",
Level = "10"
},
new Person()
{
ID = "3",
Name = "Jim",
District = "Washington",
Level = "11"
}
}; //this already has values
var grpByP = persons
.GroupBy(p => new { p.District, p.Level })
.Select(g => new
{
g.Key,
People = g.ToList<Person>()
});
foreach (var g in grpByP)
{
Console.WriteLine("Group:");
Console.WriteLine(g.Key.District);
Console.WriteLine(g.Key.Level);
Console.WriteLine("People:");
foreach (Person p in g.People)
Console.WriteLine(p.Name);
Console.WriteLine();
}
Console.ReadLine();
Output:-
Group:
Columbia 10
People:
Joe
BethGroup:
Washington 11
People:
Jim
LINQ: Converting grouped list into new list
Use SelectMany
extension method.grp
is of type IEnumerable<IGrouping<'a, Item>>
- so it some kind of list of list.
And you need return flattened list of all items.
List<Item> myDestList = grp.SelectMany(g => g).ToList();
Another approach is using your original list, because your code updating already existed items.
C# LINQ GroupBy to convert a List to a group with one property as List of values
After GroupBy
, use ToDictionary
:
source.GroupBy(x => x.Name)
.ToDictionary(x => x.Key, x => x.Select(e => e.Value).ToList());
This yields a Dictionary<string, List<string>>
where the keys are the names and the values are lists made up of projecting each element to a string
under that specific group.
I assume that Value
is a string
for example purposes only but in reality, it doesn't really matter as the solution remains the same.
Using Linq to group a list of objects into a new grouped list of list of objects
var groupedCustomerList = userList
.GroupBy(u => u.GroupID)
.Select(grp => grp.ToList())
.ToList();
LINQ groupby to List<List<object>>
I think you are looking this:
var data = Model.Where(x => x.Start.Date == DateTime.UtcNow.Date)
.GroupBy(x => new { x.Start, x.Field2 })
.Select(g=>g.ToList()).ToList();
Add a Select
call to get a list per each group
Linq: Group List of Dictionary by multiple properties and Form a List
Start with flattening the dictionary values.
var values = _allVertexes.Values.SelectMany(v => v);
Now that you have all the vertexes in one place, it is time to group them up by FeatureClassId and OID.
var grouped = values.GroupBy(v => new { v.FeatureClassId, v.OID });
All that is left now is iterating over the grouped collection and do whatever you want with them.
Alternatively, if you want two level grouping then do grouping on FeatureClassId, then group their elements by OID.
var grouped = values.GroupBy(v => v.FeatureClassId).Select(g => g.GroupBy(v => v.OID));
Group list by multiple conditions, LINQ
With ternary conditional operator
books.GroupBy(b => b.Price < 10 ? 10 : (b.Price < 20 ? 20 : 30));
C# linq group to a list inside a list
Use GroupBy
and then Select
to define how you want the grouped results projected.
List<UserManagementClass> UsersList = Users
.GroupBy(u => u.userID)
.Select(_ => new UserManagementClass
{
userID = _.Key,
UserAgencyList = _.SelectMany(u => u.UserAgencyList.Select(agency => agency)).ToList()
}).ToList();
Group By Multiple Columns
Use an anonymous type.
Eg
group x by new { x.Column1, x.Column2 }
C# Linq Group By on multiple columns
var consolidatedChildren =
from c in children
group c by new
{
c.School,
c.Friend,
c.FavoriteColor,
} into gcs
select new ConsolidatedChild()
{
School = gcs.Key.School,
Friend = gcs.Key.Friend,
FavoriteColor = gcs.Key.FavoriteColor,
Children = gcs.ToList(),
};
var consolidatedChildren =
children
.GroupBy(c => new
{
c.School,
c.Friend,
c.FavoriteColor,
})
.Select(gcs => new ConsolidatedChild()
{
School = gcs.Key.School,
Friend = gcs.Key.Friend,
FavoriteColor = gcs.Key.FavoriteColor,
Children = gcs.ToList(),
});
Related Topics
Sending Array of Bytes from Client to Server
C# Best Way to Run a Function Every Second, Timer VS Thread
Encode a Filestream to Base64 With C#
Securely Store a Password in the Application in C#
How to Select Last Record in a Linq Groupby Clause
Convert Time With Milliseconds to Datetime in C#
Regular Expression for Valid Filename
How to Programmatically Set the System Volume
How to Change a Image on a Button Using Windows Forms
Download File With Webclient or Httpclient
Using Linq to Remove Elements from a List<T>
How to Use Variable in Feature File
How to Check Byte Array Empty or Not
How to Extract Custom Header Value in Web API Message Handler
Error 5:Access Denied When Starting Windows Service