Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT COUNT in LINQ to SQL C#

Tags:

c#

sql

linq

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?

like image 872
Oshrib Avatar asked Jun 13 '11 12:06

Oshrib


2 Answers

Like that

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

or even easier

var purchCount = myBlaContext.purchases.Count(); 
like image 126
Andrei Avatar answered Sep 21 '22 13:09

Andrei


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();
like image 45
Francis Gilbert Avatar answered Sep 21 '22 13:09

Francis Gilbert