Print Page | Close Window

how get record count using linq to devforce

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1914
Printed Date: 21-Apr-2026 at 12:19pm


Topic: how get record count using linq to devforce
Posted By: ehsan
Subject: how get record count using linq to devforce
Date Posted: 25-Jun-2010 at 12:17am
I've a query like this :

IEntityQuery<testTable> query = (from r in mgr.testTables where [condition] select r);

Now I want to get count of selected records, in linq to sql its possible to do this by adding .Count() at the end of linq query.

Here how can i get count of some special records ?




Replies:
Posted By: davidklitzke
Date Posted: 25-Jun-2010 at 1:50pm

I am not sure what you mean by count of some special records, but here is an example of some code in the Learning Resources that reports a Fetch Count of an argument of type IEnumerable.

 private void ReportFetchCount(IEnumerable result, string entityType, string queryDescription) {
      int entitiesRetrieved = ((ICollection)result).Count;
      WriteMessage(string.Format("Retrieved {0} entities of type {1} using query of type {2}", entitiesRetrieved, entityType, queryDescription));
    }


Posted By: ehsan
Date Posted: 25-Jun-2010 at 10:16pm

No, I dont mean get count of an argument of type IEnumerable ! I know how do that !!!

I need to get record count of a database table (I exactly mean something like SQL COUNT function).


Posted By: ehsan
Date Posted: 27-Jun-2010 at 5:16am
No idea ???!!!


Posted By: kimj
Date Posted: 28-Jun-2010 at 12:24pm
Just as with standard LINQ or LINQ to EF, you can add the Count() operator to your query.  Count() is an "immediate execution" operator, which means you can't build a query containing the operator as you can with the usual deferred execution types of operators.  So you can do something like this: 
 
int ct = (from r in mgr.testTables where [condition] select r).Count();
 
If you're working in Silverlight then the query needs to be executed asynchronously, so you can do it like this:
      var op = (from r in mgr.testTables where [condition] select r).AsScalarAsync().Count();
      op.Completed += (o, e) => {
        int ct = e.Result;
      };


Posted By: ehsan
Date Posted: 29-Jun-2010 at 11:10pm
Thanks kimj,
That solved my problem !



Print Page | Close Window