DataSource.Table.Where(stringOfConstraint) does not exist in LinqToSQL for OData support?
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 furthe开发者_JAVA技巧r constrain the query.
If that is not supported in Linq2SQL, how can I make a similar statement?
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();
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.
精彩评论