ऑर्डर बाय का उपयोग सरणियों को आरोही या अवरोही क्रम में क्रमबद्ध करने के लिए किया जाता है
GroupBy ऑपरेटर ग्रुपिंग ऑपरेटर्स श्रेणी से संबंधित है। यह ऑपरेटर वस्तुओं का एक फ्लैट अनुक्रम लेता है, उस अनुक्रम को समूहों में व्यवस्थित करता है (IGrouping
उदाहरण
class ElectronicGoods { public int Id { get; set; } public string Name { get; set; } public string Category { get; set; } public static List<ElectronicGoods> GetElectronicItems() { return new List<ElectronicGoods>() { new ElectronicGoods { Id = 1, Name = "Mobile", Category = "Phone"}, new ElectronicGoods { Id = 2, Name = "LandLine", Category = "Phone"}, new ElectronicGoods { Id = 3, Name = "Television", Category = "TV"}, new ElectronicGoods { Id = 4, Name = "Grinder", Category = "Food"}, new ElectronicGoods { Id = 5, Name = "Mixer", Category = "Food"}, }; } } class Program { static void Main() { //Group by var res=ElectronicGoods.GetElectronicItems().GroupBy(x => x.Category).Select(x => new { Key = x.Key, electronicGoods = x.OrderBy(c => c.Name) }); foreach (var group in res) { Console.WriteLine("{0} - {1}", group.Key, group.electronicGoods.Count()); Console.WriteLine("----------"); foreach (var electronicGoods in group.electronicGoods) { Console.WriteLine(electronicGoods.Name + "\t" + electronicGoods.Category); } Console.WriteLine(); Console.WriteLine(); } Console.ReadKey(); } }
आउटपुट
Phone - 2 ---------- LandLine Phone Mobile Phone TV - 1 ---------- Television TV Food - 2 ---------- Grinder Food Mixer Food
आदेश दें
class ElectronicGoods { public int Id { get; set; } public string Name { get; set; } public string Category { get; set; } public static List<ElectronicGoods> GetElectronicItems() { return new List<ElectronicGoods>() { new ElectronicGoods { Id = 1, Name = "Mobile", Category = "Phone"}, new ElectronicGoods { Id = 2, Name = "LandLine", Category = "Phone"}, new ElectronicGoods { Id = 3, Name = "Television", Category = "TV"}, new ElectronicGoods { Id = 4, Name = "Grinder", Category = "Food"}, new ElectronicGoods { Id = 5, Name = "Mixer", Category = "Food"}, }; } } class Program { static void Main() { //Order by var res = ElectronicGoods.GetElectronicItems().OrderBy(x => x.Category); foreach (var items in res) { Console.WriteLine(items.Name + "\t" + items.Category); } Console.ReadKey(); } }
आउटपुट
Grinder Food Mixer Food Mobile Phone LandLine Phone Television TV