Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count Rows in Linq

Tags:

c#

.net

linq

I am very new in LINQ

I have written following query :

var duplicate =
    from  loginId in DataWorkspace.v2oneboxData.me_employees
    where loginId.me_login_name == this.me_login_name
              && loginId.me_pkey != this.me_pkey
    select loginId;

I want to count the rows returned in the result duplicate

I searched many of articles that says use duplicate.Count(). but i dont see count() in my intelisense

how do i count from result

like image 375
ghanshyam.mirani Avatar asked Oct 13 '11 05:10

ghanshyam.mirani


2 Answers

If you're using lambda expressions then make sure you have referenced System.Linq.Expressions namespace.

var cardCount = datatable
    .AsEnumerable()
    .Where(p => p.Field<decimal>("SpotID") ==
        Convert.ToDecimal(currentActivatedSpot))
    .Count();
like image 79
Mohan Sharma Avatar answered Sep 17 '22 14:09

Mohan Sharma


How about doing it using extension methods:

 var count = me_employees.Where(me => me.me_login_name == this.me_login_name && me.me_pkey != this.me_pkey).Count();

Even better:

var count = me_employees.Count(me => me.me_login_name == this.me_login_name && me.me_pkey != this.me_pkey);

BIG NOTE: Ensure that you have referenced System.Core. System.Data.Linq as well but I assume you already referenced it.

like image 23
John Gathogo Avatar answered Sep 19 '22 14:09

John Gathogo