Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using LINQ to count value frequency

I have a table

ID|VALUE

VALUE is an integer field with possible values between 0 and 4. How can I query the count of each value?

Ideally the result should be an array with 6 elements, one for the count of each value and the last one is the total number of rows.

like image 292
Rana Avatar asked Sep 05 '10 23:09

Rana


People also ask

How do you count in LINQ?

In LINQ, you can count the total number of elements present in the given sequence by using the Count Method. This method returns the total number of elements present in the given sequence.

Is LINQ good for performance?

It is slightly slowerLINQ syntax is typically less efficient than a foreach loop. It's good to be aware of any performance tradeoff that might occur when you use LINQ to improve the readability of your code. And if you'd like to measure the performance difference, you can use a tool like BenchmarkDotNet to do so.

How do you find the minimum and maximum value in LINQ?

In LINQ, you can find the minimum element of the given sequence by using Min() function. This method provides the minimum element of the given set of values. It does not support query syntax in C#, but it supports in VB.NET. It is available in both Enumerable and Queryable classes in C#.

What is LINQ to DB?

LINQ to DB is the fastest LINQ database access library offering a simple, light, fast, and type-safe layer between your POCO objects and your database.


1 Answers

This simple program does just that:

class Record
{
    public int Id { get; set; }
    public int Value { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        List<Record> records = new List<Record>()
        {
            new Record() { Id = 1,  Value = 0},
            new Record() { Id = 2,  Value = 1 },
            new Record() { Id = 3,  Value = 2 },
            new Record() { Id = 4,  Value = 3 },
            new Record() { Id = 5,  Value = 4 },
            new Record() { Id = 6,  Value = 2 },
            new Record() { Id = 7,  Value = 3 },
            new Record() { Id = 8,  Value = 1 },
            new Record() { Id = 9,  Value = 0 },
            new Record() { Id = 10, Value = 4 }
        };

        var query = from r in records
                    group r by r.Value into g
                    select new {Count = g.Count(), Value = g.Key};

        foreach (var v in query)
        {
            Console.WriteLine("Value = {0}, Count = {1}", v.Value, v.Count);
        }
    }
}

Output:

Value = 0, Count = 2
Value = 1, Count = 2
Value = 2, Count = 2
Value = 3, Count = 2
Value = 4, Count = 2

Slightly modified version to return an Array with only the count of values:

int[] valuesCounted  = (from r in records
                        group r by r.Value
                        into g
                        select g.Count()).ToArray();

Adding the rows count in the end:

valuesCounted = valuesCounted.Concat(new[] { records.Count()}).ToArray();
like image 71
Leniel Maccaferri Avatar answered Oct 03 '22 21:10

Leniel Maccaferri