Oshrib
Oshrib

Reputation: 1900

SELECT COUNT in LINQ to SQL C#

How can I write LINQ to SQL with COUNT?

Example:

var purch = from purchase in myBlaContext.purchases
            select purchase;

How can I get the count here?

Upvotes: 49

Views: 203607

Answers (2)

Andrei
Andrei

Reputation: 56726

Like that

var purchCount = (from purchase in myBlaContext.purchases select purchase).Count();

or even easier

var purchCount = myBlaContext.purchases.Count();

Upvotes: 91

Francis Gilbert
Francis Gilbert

Reputation: 3442

You should be able to do the count on the purch variable:

purch.Count();

e.g.

var purch = from purchase in myBlaContext.purchases
select purchase;

purch.Count();

Upvotes: 10

Related Questions