In Linq, how do you conduct a basic count?

ado.net entity-framework linq linq-to-entities

Question

I intended to create a table with pagination, but the NeerDinner example gets all of the data into aPaggingList I omitted that step since I needed to acquire more than 10,000 rows of data of a certain kind.

so I ask this question instead

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);

The issue is that before theTake part?

I was considering:

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 would make the question redundant.

Anyone has ideas on how I could Make this happen well.?

1
6
12/2/2010 9:27:48 AM

Accepted Answer

If you interpret a question as such:

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

LINQ to SQL is intelligent enough to turn this into aSELECT COUNT This has the potential to be very effective (efficiency will depend more on the conditions in the query). In the end, the count action takes place in the database rather than in LINQ code.

17
12/2/2010 9:35:02 AM

Popular Answer

You may also add query logic to the count; for an example, check the example below.

 public int GetTotalCountForAllEmployeesByReportsTo(int? reportsTo, string orderBy = default(string), int startRowIndex = default(int), int maximumRows = default(int))
        {
            //Validate Input
            if (reportsTo.IsEmpty())
                return GetTotalCountForAllEmployees(orderBy, startRowIndex, maximumRows);

            return _DatabaseContext.Employees.Count(employee => reportsTo == null ? employee.ReportsTo == null : employee.ReportsTo == reportsTo);
        }


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow