Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a simple Count in Linq?

I wanted to do a paging style table, but NeerDinner example fetches the entire data into a PaggingList type, and I have more than 10 000 rows to be fetched, so I skipped that part.

so I come up with this query

var r = (from p in db.Prizes
            join c in db.Calendars on p.calendar_id equals c.calendar_id
            join ch in db.Challenges on c.calendar_id equals ch.calendar_id
            join ca in db.ChallengeAnswers on ch.challenge_id equals ca.challenge_id
            join cr in db.ChallengeResponses on ca.challenge_answer_id equals cr.challenge_answer_id

            where
                p.prize_id.Equals(prizeId)
                && ch.day >= p.from_day && ch.day <= p.to_day
                && ca.correct.Equals(true)
                && ch.day.Equals(day)

            orderby cr.Subscribers.name

            select new PossibleWinner()
            {
                Name = cr.Subscribers.name,
                Email = cr.Subscribers.email,
                SubscriberId = cr.subscriber_id,
                ChallengeDay = ch.day,
                Question = ch.question,
                Answer = ca.answer
            })
        .Skip(size * page)
        .Take(size);

Problem is, how can I get the total number of results before the Take part?

I was thinking of:

var t = (from p in db.JK_Prizes
            join c in db.JK_Calendars on p.calendar_id equals c.calendar_id
            join ch in db.JK_Challenges on c.calendar_id equals ch.calendar_id
            join ca in db.JK_ChallengeAnswers on ch.challenge_id equals ca.challenge_id
            join cr in db.JK_ChallengeResponses on ca.challenge_answer_id equals cr.challenge_answer_id

            where
                p.prize_id.Equals(prizeId)
                && ch.day >= p.from_day && ch.day <= p.to_day
                && ca.correct.Equals(true)
                && ch.day.Equals(day)

            select cr.subscriber_id)
        .Count();

but that will do the query all over again...

anyone has suggestions on how can I do this effectively ?

like image 792
balexandre Avatar asked Dec 02 '10 09:12

balexandre


People also ask

How to Count in LINQ Query?

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.

How to calculate Count in LINQ c#?

The Linq Count Method used to return the number of elements present in the collection or the number of elements that have satisfied a given condition. There are two overloaded versions of the Linq Count() extension method is available as shown below.

How do you count in IQueryable?

Please use the Count() method. IQueryable<People> list = repository. FindAllPeople; int cnt = list. Count();

How do you sum in Linq?

In LINQ, you can find the sum of the given numeric elements by using the Sum() method. This method calculates the sum of the numeric value present in the given sequence. 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#.


1 Answers

If you take a query as such:

var qry = (from x in y
           select x).Count();

...LINQ to SQL will be clever enough to make this a SELECT COUNT query, which is potentially rather efficient (efficiency will depend more on the conditions in the query). Bottom line is that the count operation happens in the database, not in LINQ code.

like image 150
Fredrik Mörk Avatar answered Sep 17 '22 14:09

Fredrik Mörk