How do I use group by in LINQ?
Category: System.Linq, viewed: 108 time(s).
This example show you how we can categorize objects for a specific object's field. We are going to use the LINQ group by keyword to group orders by their customer.
group o by o.CustomerId means that we want to group the orders by customer identification. The o between the group and by keywords mean that we want to get all fields from the orders. If you want to get a specific field instead you can write something like group o.ArticleId by o.CustomerId to get the order article id field.
using System;
using System.Collections.Generic;
using System.Linq;
namespace Kodecsharp.Example.Linq
{
class LinqGroupDemo
{
[STAThread]
public static void Main(string[] args)
{
List<Order> orders = new List<Order>() {
new Order(1, 1, 1, 1, 1500.00f),
new Order(2, 3, 1, 4, 450.00f),
new Order(3, 2, 3, 5, 349.00f),
new Order(4, 3, 2, 1, 400.00f),
new Order(5, 4, 3, 2, 550.00f),
new Order(6, 4, 1, 2, 550.00f),
new Order(7, 4, 2, 4, 550.00f),
new Order(8, 5, 1, 1, 250.00f),
};
var query = from o in orders
group o by o.CustomerId;
foreach (var result in query)
{
Console.WriteLine("Customer Id: " + result.Key);
foreach (var order in result)
{
Console.WriteLine("Order Id: " + order.Id
+ ", Article Id: " + order.ArticleId
+ ", Quantity: " + order.Quantity
+ ", Unit Price: " + order.UnitPrice);
}
}
Console.ReadLine();
}
}
class Order
{
private long id;
private long articleId;
private long customerId;
private int quantity;
private float unitPrice;
public Order(long id, long articleId, long customerId, int quantity, float unitPrice)
{
this.id = id;
this.articleId = articleId;
this.customerId = customerId;
this.quantity = quantity;
this.unitPrice = unitPrice;
}
public long Id
{
get { return this.id; }
set { this.id = value; }
}
public long ArticleId
{
get { return this.articleId; }
set { this.articleId = value; }
}
public long CustomerId
{
get { return this.customerId; }
set { this.customerId = value; }
}
public int Quantity
{
get { return this.quantity; }
set { this.quantity = value; }
}
public float UnitPrice
{
get { return this.unitPrice; }
set { this.unitPrice = value; }
}
}
}
Here are the output of the program:
Customer Id: 1
Order Id: 1, Article Id: 1, Quantity: 1, Unit Price: 1500
Order Id: 2, Article Id: 3, Quantity: 4, Unit Price: 450
Order Id: 6, Article Id: 4, Quantity: 2, Unit Price: 550
Order Id: 8, Article Id: 5, Quantity: 1, Unit Price: 250
Customer Id: 3
Order Id: 3, Article Id: 2, Quantity: 5, Unit Price: 349
Order Id: 5, Article Id: 4, Quantity: 2, Unit Price: 550
Customer Id: 2
Order Id: 4, Article Id: 3, Quantity: 1, Unit Price: 400
Order Id: 7, Article Id: 4, Quantity: 4, Unit Price: 550
Download Hundreds of Complimentary Industry Resources
Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more;
all available at no cost to you. With more than 600 complimentary offers, you'll find
plenty of titles to suit your professional interests and needs.
Click Here and Sign up today!