L
L
Ledington2022-01-25 10:01:39
C++ / C#
Ledington, 2022-01-25 10:01:39

How to count the number in a group?

There is a list of films and each has its own rating. I sorted them by rating groups and I'm trying to calculate the number by rating, i.e. how many in each movie rating. How to do it?

public static List<DVDrental> GetDataRating()
        {
            using (var db = new LinqToDB.Data.DataConnection(LinqToDB.ProviderName.PostgreSQL, Config.SqlConnectionString))
            {
                var table = db.GetTable<DVDrental>();
                var list =
                table.GroupBy(g => new { g.Rating })
                .Select(s => new DVDrental { Rating = s.Key.Rating })
                .ToList();
                return list;
            }
        }


In main:
var ratings = Core.GetDataRating();
foreach (var rating in ratings)
            {   
                Console.WriteLine($"Рейтинг: {rating.Rating}");                
            }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
twobomb, 2022-01-25
@twobomb

var list =
                table.GroupBy(g => new { g.Rating })
                .Select(f => new int[] { f.Key.Rating, table.Count(f1 => f1.Rating.Equals(f.Key.Rating)) })
                .ToList();
            foreach (var l in list)
                Console.WriteLine("Рейинг {0} - количество {1}",l[0],l[1]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question