Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Group by Example
//Title of this code //Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //Your code goes here var orders = new List<Order>(); orders.Add(new Order(){Id=1, Product = "1"}); orders.Add(new Order(){Id=2, Product = "2"}); orders.Add(new Order(){Id=3, Product = "1"}); orders.Add(new Order(){Id=4, Product = "3"}); orders.Add(new Order(){Id=5, Product = "3"}); orders.Add(new Order(){Id=6, Product = "1"}); orders.Add(new Order(){Id=7, Product = "1"}); orders.Add(new Order(){Id=8, Product = "1"}); var groupedOrders = orders.GroupBy(o => o.Product); // Returns an IGrouping where key is the id and the value is IEnumerable of orders var sortedGroups = groupedOrders.OrderByDescending(g => g.Count()); // Sorts the groups descending by count of the IEnumerable of orders var projectedOrders = sortedGroups.Select(x => new CustomerOrders(){OrderId = x.First().Id, // Projects each of the groups into a new CustomerOrders Product = x.First().Product, Count = x.Count()}); System.Console.Write(projectedOrders.Last().OrderId); } public class Customers { public Guid Id { get; set; } public string FirstName{ get; set; } public string LastName{ get; set; } } public class Order { public int Id { get; set; } public Guid CustomerId { get; set; } public string Product { get; set; } public DateTime Date { get; set; } } public class CustomerOrders { public int OrderId { get; set; } public string Product { get; set; } public int Count { get; set; } } } }
run
|
edit
|
history
|
help
0
octalofint
Write a program to filter distinct items from the array
Array Foreach with sum
display xml : linq
Interface IEnumerable
switch it
test
Find Largest Number
MegaWays
Knapsack Problem - recursive