Group By Linq
- powerful feature of linq Similar to Group by as SQL
- It can be done using both linq as well as lambda expression.
using System;
using System.Collections.Generic;
using System.Linq;
namespace XML
{
class Program
{
public class student
{
public int id { get; set; }
public string name { get; set; }
public int classId { get; set; }
public List<student> getStudents()
{
List<student> obj = new List<student>();
obj.Add(new student { id = 101, name = "abc1", classId = 1 });
obj.Add(new student { id = 102, name = "abc2", classId = 1 });
obj.Add(new student { id = 103, name = "abc3", classId = 2 });
obj.Add(new student { id = 104, name = "abc4", classId = 2 });
obj.Add(new student { id = 105, name = "abc5", classId = 2 });
obj.Add(new student { id = 106, name = "abc6", classId = 3 });
obj.Add(new student { id = 107, name = "abc7", classId = 3 });
obj.Add(new student { id = 108, name = "abc8", classId = 3 });
obj.Add(new student { id = 109, name = "abc9", classId = 3 });
return obj;
}
}
static void Main(string[] args)
{
student s = new student();
var students = s.getStudents().ToList();
var final = students.GroupBy(x => x.classId).ToDictionary(y => y.Key, y => y.ToList());
foreach (var x in final)
{
Console.WriteLine("----" + x.Key + "-----");
foreach (var item in x.Value)
{
Console.WriteLine(item.id);
Console.WriteLine(item.name);
Console.WriteLine();
}
Console.WriteLine("===================================");
}
Console.WriteLine("--------Slight Modification----------");
var final1 = students.GroupBy(x => x.classId).Select(g => new { Key=g.Key, List=g.ToList() });
foreach (var item in final1)
{
Console.WriteLine("----" + item.Key + "-----");
foreach (var item1 in item.List)
{
Console.WriteLine(item1.id);
Console.WriteLine(item1.name);
Console.WriteLine();
}
}
Console.ReadLine();
}
}
}
Group By and Sum
Code
Code
using System;
using System.Collections.Generic;
using System.Linq;
namespace LINQ
{
class Program
{
public class student
{
public int id { get; set; }
public string name { get; set; }
public int classId { get; set; }
public int Salary { get; set; }
public List<student> getStudents()
{
List<student> obj = new List<student>();
obj.Add(new student { id = 101, name = "abc1", classId = 1, Salary = 100 });
obj.Add(new student { id = 102, name = "abc2", classId = 1, Salary = 200 });
obj.Add(new student { id = 103, name = "abc3", classId = 2, Salary = 300 });
obj.Add(new student { id = 104, name = "abc4", classId = 2, Salary = 800 });
obj.Add(new student { id = 105, name = "abc5", classId = 2, Salary = 900 });
obj.Add(new student { id = 106, name = "abc6", classId = 3, Salary = 250 });
obj.Add(new student { id = 107, name = "abc7", classId = 3, Salary = 260 });
obj.Add(new student { id = 108, name = "abc8", classId = 3, Salary = 290 });
return obj;
}
}
static void Main(string[] args)
{
student s = new student();
var students = s.getStudents().ToList();
var z = students.GroupBy(x => x.classId).Select(g => new {ClassId=g.Key, TotalSalary= g.Sum(x=>x.Salary) });
foreach (var item in z)
{
Console.WriteLine(item.ClassId);
Console.WriteLine(item.TotalSalary);
Console.WriteLine();
}
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
namespace LINQ
{
class Program
{
class Parent
{
public int Id { get; set; }
public string Val { get; set; }
}
class Child
{
public int Id { get; set; }
public string ChildVal { get; set; }
}
static void Main(string[] args)
{
List<Parent> plist = new List<Parent>() {
new Parent{ Id=1, Val="X"},
new Parent{ Id=2, Val="Y"},
new Parent{ Id=3, Val="Z"}
};
List<Child> clist = new List<Child>() {
new Child{ Id=1, ChildVal="X1"},
new Child{ Id=1, ChildVal="X2"},
new Child{ Id=1, ChildVal="X3"},
new Child{ Id=2, ChildVal="Y1"},
new Child{ Id=2, ChildVal="Y2"},
new Child{ Id=4, ChildVal="Y3"},
};
var joinedResult = from p in plist
join c in clist on p.Id equals c.Id
select new { p.Val, c.ChildVal };
Console.WriteLine("===JOIN===");
foreach (var item in joinedResult)
{
Console.WriteLine(item.Val);
Console.WriteLine(item.ChildVal);
Console.WriteLine();
}
Console.WriteLine("===GROUPJOIN===");
var y = from p in plist
join c in clist on p.Id equals c.Id into g
select new { Parent = p, Children = g };
foreach (var item in y)
{
Console.WriteLine(item.Parent.Id);
foreach (var item1 in item.Children)
{
Console.WriteLine(item1.ChildVal);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}