See the below code:-
class Order { public int Id; public string Name; } class OrderItem { public int Id; public string Name; public int OrderId; } public class Program { static void Main(string[] args) { var orders = new List<Order> { new Order { Id = 1, Name = "One" } , new Order { Id = 2, Name = "Two" } }; List<OrderItem> orderItems = new List<OrderItem> { new OrderItem { Id = 1, Name = "Item One", OrderId = 1 }, new OrderItem { Id = 2, Name = "Item Two", OrderId = 1 }, new OrderItem { Id = 3, Name = "Item Three", OrderId = 1 }, new OrderItem { Id = 4, Name = "Item Four", OrderId = 2 }, new OrderItem { Id = 5, Name = "Item Five", OrderId = 2 } }; var results = from order in orders join orderItem in orderItems on order.Id equals orderItem.OrderId into GroupOrderItems select new { order, GroupOrderItems }; //Just to write the results to the console string columns = "{0,-20} {1, -20}"; Console.WriteLine(string.Format(columns, "Order", "Item Count")); foreach (var result in results) { Console.WriteLine(columns, result.order.Name, result.GroupOrderItems.Count()); } Console.ReadLine(); } }
class Order
{
public int Id;
public string Name;
}
class OrderItem
public int OrderId;
public class Program
static void Main(string[] args)
var orders = new List<Order> {
new Order { Id = 1, Name = "One" } ,
new Order { Id = 2, Name = "Two" }
};
List<OrderItem> orderItems = new List<OrderItem>
new OrderItem { Id = 1, Name = "Item One", OrderId = 1 },
new OrderItem { Id = 2, Name = "Item Two", OrderId = 1 },
new OrderItem { Id = 3, Name = "Item Three", OrderId = 1 },
new OrderItem { Id = 4, Name = "Item Four", OrderId = 2 },
new OrderItem { Id = 5, Name = "Item Five", OrderId = 2 }
var results = from order in orders
join orderItem in orderItems on order.Id equals orderItem.OrderId into GroupOrderItems
select new { order, GroupOrderItems };
//Just to write the results to the console
string columns = "{0,-20} {1, -20}";
Console.WriteLine(string.Format(columns, "Order", "Item Count"));
foreach (var result in results)
Console.WriteLine(columns, result.order.Name, result.GroupOrderItems.Count());
Console.ReadLine();
class Order { public int Id; public string Name; public Order(int id, string name) { this.Id = id; this.Name = name; } } class OrderItem { public int Id; public string Name; public int OrderId; public OrderItem(int id, string name, int orderId) { this.Id = id; this.Name = name; this.OrderId = orderId; } } class Program { static void Main(string[] args) { List orders = new List() { new Order(1, "one"), new Order(2, "two") }; List orderItems = new List() { new OrderItem(1, "itemOne", 1), new OrderItem(2, "itemTwo", 1), new OrderItem(3, "itemThree", 1), new OrderItem(4, "itemFour", 2), new OrderItem(5, "itemFive", 2) }; var joined = from o in orders join oi in orderItems on o.Id equals oi.OrderId into gj // gj means group join and is a collection OrderItem select new { o, gj }; // this is just to write the results to the console string columns = "{0,-20} {1, -20}"; Console.WriteLine(string.Format(columns, "Order", "Item Count")); foreach (var j in joined) { Console.WriteLine(columns, j.o.Name, j.gj.Count()); } } }
public static void LinqJoin() { IList orderItems = new List() { new OrderItems(){orderId=1, orderName=”Soap”}, new OrderItems(){orderId=2, orderName=”Shampoo”}, new OrderItems(){orderId=3, orderName=”Moisturiser”} }; IList orderCount = new List(){ new OrderCount(){orderId=1, count=3}, new OrderCount(){orderId=2, count=4}, new OrderCount(){orderId=3, count=8} }; var joinResult= from items in orderItems join oc in orderCount on items.orderId equals oc.orderId select new { orderItem=items.orderName, count= oc.count } ; foreach(var item in joinResult) { Console.WriteLine(item.orderItem + “ :”+ item.count); } }
public class OrderCount{ public int orderId { get; set; } public int count { get; set; }}public class OrderItems{ public int orderId{ get; set; } public string orderName{ get; set; }}
public class OrderCount
public int orderId { get; set; }
public int count { get; set; }
public class OrderItems
public int orderId{ get; set; }
public string orderName{ get; set; }