Click here to Skip to main content
14,970,219 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to count how many values from each Columns. but I know how to use Sql query,

but I don't know how to use, C# from I was using Entityframwork.

I tried some of C# coding, but it is now working for me.

In C# coding, my Counts is always '0'.

Please help me.

What I have tried:

    SUM(CASE WHEN [Column1] = 'jin' THEN 1 ELSE 0 END +
        CASE WHEN [Column2] = 'jin' THEN 1 ELSE 0 END +
        CASE WHEN [Column3] = 'jin' THEN 1 ELSE 0 END) AS COUNTS

Gentle db = new Gentle();
           Time ec = new Time();

            for (int i = 0; i < 4; i++)
                if (ec.Column1 == "jin")
                    counts= counts + 1;

Updated 2-Jul-20 7:12am
Richard Deeming 3-Jul-20 7:05am
If your SQL query is returning 0, then there are no matching records in your table. You will need to check your data to find out what the problem is - we can't do that for you.

Alternatively, if the query returns the correct value but you're struggling to call it from C#, you'll need to show the code you've tried and explain where you're stuck.

1 solution


Can you try like this

First you need to load values to a datatable , then

int count = dt.Select().Where(s => s["Column1"].ToString().ToLower() == "jin").Count();

If you need check multiple columns then repeat the step ang find the total

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