This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
I'm binding a Telerik web grid control on the client to a web service that returns json. The grid makes a call to the web service with several parameters like this: public List<EducationCourseDto> GetData(int startIndex, int maximumRows, string sortExpression, string filterExpression) ... The sortExpression is a string that looks like this: "Description ASC" I can't control what the grid passes in but I need to use it to order the query. Does Lightspeed support this kind of syntax as part of a linq query? My code looks like this:
var courses = from m in uow.EducationCourses courses = courses.OrderBy(sortExpression); but the compiler shows this error:
The type arguments for method 'System.Linq.Queryable.OrderBy<TSource,TKey>(System.Linq.IQueryable<TSource>, System.Linq.Expressions.Expression<System.Func<TSource,TKey>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. I hope this makes sense, thanks so much for you help. |
|
|
I found the solution. By including the Dynamic Linq class from Microsoft (read about it and download it here) I was able to pass a string into the Where method: courses = courses.Where("Description.Contains(\"something\")") The Dynamic Linq class enables creating dynamic linq queries that can be created at runtime. Very nice. |
|
|
The LINQ infrastructure itself doesn't support string ordering expressions, only lambdas (technically, instances of Expression<Func<TSource,TKey>>). This is a limitation of the LINQ spec itself, not a LightSpeed issue. You will therefore need to parse the string expression sent by the Telerik grid. Fortunately, this should be trivial because property names can't contain spaces (or commas, if the grid supports multiple ordering clauses), so String.Split should be fully reliable. There is also the Dynamic LINQ library which does support string order clauses and can be backed onto any LINQ provider including LightSpeed. |
|
|
Doh! Didn't realise you'd already found it. That's what I get for replying before reading all my email. |
|