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();
Using Linq to group a list of objects into a new grouped list of list of objects with 4 objects in each group
May be this could Help.
Source:
public class Job
{
public int JobID { get; set; }
public static Job[] GetJobs()
{
var jobList = new[] {
new Job { JobID = 1 },
new Job { JobID = 2},
new Job { JobID = 3},
new Job { JobID = 4},
new Job { JobID = 5},
new Job { JobID = 6},
new Job { JobID = 7},
new Job { JobID = 8},
new Job { JobID = 9},
new Job { JobID = 10}
};
return jobList;
}
public override string ToString()
{
return $"Job {{ JobID={JobID} }}";
}
}
Group Query Using Linq:
//Data Source
var source = Job.GetJobs();
//Query Creation
var query = from index in Enumerable.Range(0, source.Length)
group source[index] by index / 4;
//Query Execution
foreach (var item in query)
{
Console.WriteLine($"Group : {item.Key}");
item.ToList().ForEach(it => Console.WriteLine(it.ToString()));
}
Console.ReadLine();
Expected Ouput:
Using Linq to group a list of objects that contains primitives data into a new grouped list of objects
You can just order type name by descending after Select
, it'll give you the desired output - (string, integer, double, character) items
list.GroupBy(x => x.GetType())
.OrderByDescending(g => g.Key.Name)
.Select(grp => grp.ToList())
.ToList()
.ForEach(group => group.ForEach(Console.WriteLine));
Another option is order by IsPrimitive
property OrderBy(g => g.Key.IsPrimitive)
, for your particular data it also gives the desired output
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.
LINQ group data into a list of objects containing a list + selecting specific original data
Here is how you can write it :
return db.persondatas
//.Where(/* ... */)
.GroupBy(x => x.name, (name, g) => new ListData
{
label = name,
data = g.GroupBy(d => d.x, (x, data) => new Point
{
x = x,
y = data.Sum(d => d.y)
})
.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
Pass LINQ Group By Data Into New List Of Objects
For the property initializer to work you need a parameterless constructor and your fields should be properties to be accessible:
public class DataPoint
{
[DataMember(Name = "y")]
public Nullable<double> Y { get; set; };
[DataMember(Name = "label")]
public string Label { get; set; }
public DataPoint()
{
}
public DataPoint(double y, string label)
{
Y = y;
Label = label;
}
}
and now your code should compile, alternatively use the constructor with parameters as someone suggested in comments.
C# - Lambda - grouping List data containing nested List objects
I usually do it like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
List<dDepartment> depts = new List<dDepartment>();
var results = depts
.SelectMany(g => g.Rooms
.SelectMany(s => s.Elements.Select( e => new {
deptId = g.DepartmentId,
deptName = g.DepartmentName,
roomId = s.RoomId,
roomName = s.RoomName,
elementId = e.ElementId,
elementName = e.ElementName
})
)).GroupBy(g => new {g.deptId, g.roomId})
.ToList();
}
}
public class dDepartment
{
public int DepartmentId { get; set; }
public string DepartmentName { get; set; }
public List<dRoom> Rooms { get; set; }
}
public class dRoom
{
public int RoomId { get; set; }
public string RoomName { get; set; }
public List<dElement> Elements { get; set; }
}
public class dElement
{
public int ElementId { get; set; }
public string ElementName { get; set; }
public List<dAction> Actions { get; set; }
}
public class dAction
{
public int ActionId { get; set; }
public string ActionName { get; set; }
public int TimeNorm { get; set; }
public int ElementCount { get; set; }
}
}
Related Topics
Entity Framework Join 3 Tables
Associating Enums with Strings in C#
Javascriptserializer.Deserialize - How to Change Field Names
Difference Between Observablecollection and Bindinglist
Why Can't I Use the 'Await' Operator Within the Body of a Lock Statement
Routing with Multiple Parameters Using ASP.NET MVC
How to Check for Nulls in an '==' Operator Overload Without Infinite Recursion
How to Dump Entire Objects to a Log in C#
Web App Blocked While Processing Another Web App on Sharing Same Session
Differencebetween Debug and Release in Visual Studio
Best Practices for Exception Management in Java or C#
Hide Console Window from Process.Start C#
How to Update Gui with Backgroundworker