views:

18

answers:

2

I'm looking at this Telerik demo, and am unable to get the "count" statement to work. I believe that the following command is only supported on EntityFramework, and not Linq2SQL.

return  CurrentDataSource.Products.Where(where).Count();

The parameter "where" in lowercase is actually a string that is passed in the ADO.Net DataServices (OData) url. This URL should be sent to the Linq provider to further constrain the query.

If that is not supported in Linq2SQL, how can I make a similar statement?

A: 

You could write your own generic extension, how you should write the most effective one depends on the situation... but it should probably be something like:

public static class IQueryableExtensions 
{
    public static int Count(this IQueryable<T> query)
    {
       return query.ToList().Count;
    }
}

This is probably not the most effective way and only works if you have an IQueryable. If performance is not a problem just use it.

mastoj
+1  A: 

You need to use a lambda expression for the predicate used by the Queryable.Where method:

return CurrentDataSource.Products.Where(p => p.Id > 100).Count();

If you really want to use a string then take a look at Dynamic LINQ, starting with Scott Gu's blog post: Dynamic LINQ (Part 1: Using the LINQ Dynamic Query Library). Then you should be able to write queries similar to:

return CurrentDataSource.Products.Where("Id > 100").Count();
Ahmad Mageed