Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
List<Mobiles> mobiles = new List<Mobiles>()
            {
                new Mobiles { MobileID = 1, MobileName = "Samsung S21", Price = 19000, Network = "4G" },
                new Mobiles { MobileID = 3, MobileName = "Nokia C2", Price = 19000, Network = "4G" },
                new Mobiles { MobileID = 2, MobileName = "Motorola G40", Price = 29000, Network = "5G" },
                new Mobiles { MobileID = 4, MobileName = "Samsung A24", Price = 99000, Network = "4G" },
                new Mobiles { MobileID = 7, MobileName = "Samsung S33", Price = 89000, Network = "4G" },
                new Mobiles { MobileID = 6, MobileName = "Realme XT", Price = 77000, Network = "5G" },
                new Mobiles { MobileID = 8, MobileName = "Realme Narzo", Price = 67000, Network = "5G" },
                new Mobiles { MobileID = 11, MobileName = "Vivo V17", Price = 77000, Network = "4G" }
            };


What I have tried:

mobiles.GroupBy(x => x.Price)
                .Select(x => new
                {
                    Name = x.Key
                    ,
                    Price = x.Count()
                })
                .Where(x => x.Price > 1)
                .ToList().ForEach(x => Console.WriteLine(x.Name+" "+x.Price));



In the foreach section I am unable to access the MobileName field. Please help?
Posted
Updated 9-Aug-23 2:03am

 
Share this answer
 
Try:
C#
var groupedMobiles = mobiles.GroupBy(m => m.Price, (key, items) => new
{
    Price = key,
    Items = items.OrderBy(m => m.MobileName).ToList(),
});

foreach (var group in groupedMobiles)
{
    Console.WriteLine("Price: {0}", group.Price);
    foreach (Mobiles mobile in group.Items)
    {
        Console.WriteLine("* {0} ({1})", mobile.MobileName, mobile.Network);
    }
    Console.WriteLine();
}
 
Share this answer
 
Comments
George Swan 9-Aug-23 10:24am    
An excellent solution. Could I suggest that the ToList method call is not needed and can be omitted?
Samriddha Chowdhury 10-Aug-23 2:13am    
Hi could you please explain what this piece of code is doing?

(key, items) => new
{
Price = key,
Items = items.OrderBy(m => m.MobileName).ToList(),
});
Richard Deeming 10-Aug-23 3:45am    
That's a lambda expression used as the resultSelector parameter of the GroupBy method:
Enumerable.GroupBy Method (System.Linq) | Microsoft Learn[^]

For each group of items, it is passed the key of the group and the list of items in that group. It then maps those to an anonymous object which is the type of item in the returned sequence.

The alternative would be to group and then select:
mobiles.GroupBy(m => m.Price).Select(group => new
{
    Price = group.Key,
    Items = group.OrderBy(m => m.MobileName).ToList(),
});
Samriddha Chowdhury 11-Aug-23 7:40am    
thank you so much. this one is easier for me
You don't include it in the Select - just Name and Price so you can't access it in the results: it doesn't exist in the new anonymous type you created.

And you can't add it to the type directly because the Group contains multiple entries for the Mobile name!
What you need is to create a Dictionary of sublists, which can take a bit of getting your head round - it took me a while when I needed to do just that. So I wrote a quick tip on how to do it: Using Linq to create a Dictionary of sub-Lists by grouping from a collection.[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900