score:17
try this... (but i'm not sure it will do what you want.)
public static class extensions
{
public static iqueryable<t> sort<t>(this iqueryable<t> query,
string sortfield,
sortdirection direction)
{
if (direction == sortdirection.ascending)
return query.orderby(s => s.gettype()
.getproperty(sortfield));
return query.orderbydescending(s => s.gettype()
.getproperty(sortfield));
}
}
... the generic parameter should be on the method and not on the class. moving it from extensions<t>
to sort<t>(
would allow you to get rid of the compiler error you are having.
as for what you are trying to do with reflection, you would be returning a propertyinfo object for the orderby clause. this is most likely not compable with the expression tree that you want. you may want to look at dynamic linq.
... this is an extract from dynamic linq.
public static iqueryable orderby(this iqueryable source,
string ordering,
params object[] values) {
if (source == null) throw new argumentnullexception("source");
if (ordering == null) throw new argumentnullexception("ordering");
parameterexpression[] parameters = new parameterexpression[] {
expression.parameter(source.elementtype, "") };
expressionparser parser = new expressionparser(parameters,
ordering,
values);
ienumerable<dynamicordering> orderings = parser.parseordering();
expression queryexpr = source.expression;
string methodasc = "orderby";
string methoddesc = "orderbydescending";
foreach (dynamicordering o in orderings) {
queryexpr = expression.call(
typeof(queryable), o.ascending ? methodasc : methoddesc,
new type[] { source.elementtype, o.selector.type },
queryexpr, expression.quote(expression.lambda(o.selector,
parameters)));
methodasc = "thenby";
methoddesc = "thenbydescending";
}
return source.provider.createquery(queryexpr);
}
score:3
the error already tells you:
extension methods must be defined in a non-generic static class.
simply remove the generic type argument.
public static class extensions // no <t>
{
// ...
}
score:8
change this:
public static class extensions
{
public static iqueryable<t> sort<t>(this iqueryable<t> query, string sortfield, sortdirection direction)
{
//code
}
}
the class needs to be non-generic, just your extension method should be :)
Source: stackoverflow.com
Related Query
- IQueryable<T> Extension Method not working
- Extension method where and select not working
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- IQueryable<T> does not contain a definition for 'Include' and no extension method 'Include'
- LinqPad Linq Include() extension method is not found even after adding references
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- System.Xml.Linq.XElement>' does not contain a definition for 'First' and no extension method 'First' accepting a first argument of
- c# Linq `List<Interface>.AddRange` Method Not Working
- Add a LINQ or DBContext extension method to get an element if not exist then create with data in predicate (FirstOrCreate)
- 'IEnumerable<>' does not contain a definition for '' and no extension method '' accepting a first argument of type 'IEnumerable<>' could be found
- Extension method not found (not an assembly reference issue)
- EF Core Include method not working
- LINQ to Entities Method Not Recognized - Extension Method Style
- Nhibernate linq. The where extension method does not add the where clause to the SQL command, why?
- Entity framework core Output parameter not working in store procedure using FromSql method (Linq)
- Ravendb Session.Query<T>() method does not return any result when sending Expression<Func<T, bool>> to Where() Extension method
- LINQ to Entities: queryable extension method not reconized inside where condition
- Outer Join is not working in Linq Query: The method 'Join' cannot follow the method 'SelectMany' or is not supported
- Extension Method not compiling (No defintion for type 'string')
- Does not contain a definition for 'where' and the best extension method overload
- CS1061: 'IEnumerable<>' does not contain a definition for '' and no extension method '' accepting a first argument of type 'IEnumerable<>'
- Extension method & LINQ to Entities does not recognize the method error
- 'int' does not contain a definition for 'Contains' and the best extension method overload 'Queryable.Contains
- VB.NET linq extension methods such as ToList not working with generics
- Linq extension method select not in
- C# CopyToDataTable method not working
- Linq in conjunction with custom generic extension method build error - An expression tree may not contain an assignment operator?
- int' does not contain a definition for 'contains' and no extension method 'contains' accepting a first argument of type 'int' could be found
- C# Select Overload Method With Accumulator Not Working
- Azure CosmosDB Contains method does not working
More Query from same tag
- How to deep look into all possible linked objects?
- Use a variable table name for Entity Framework SqlQuery to fetch data into entity Class
- Filtering a collection deep in an object graph
- Reusing projections in queries, the part after "select new"
- Group By records from csv file
- Nothing gets added to Object list
- OData concern for SQL injection
- LINQ to XML parsing single object
- How to set multiple values in a list using lambda expression?
- Intersect between two lists not working
- The cast to value type 'Int32' failed because the materialized value is null
- Creating dynamic Expression<Func<T,Y>>
- LINQ set limit within the result
- How to select the last review for each item using LINQ-To-Entities?
- Entity framework select with group by
- Read XML File Using Linq is not reading element
- How to combine a linq query dynamically?
- How to return the result set with columns with Linq
- Is there a "split list" method in c#?
- How to reuse a variable declared by "var" in C#
- Three-Tier Architecture: Get All Data and Validations
- error: no match for ‘operator=’ (operand types are ‘const yyy’ and ‘std::remove_reference<const yyy&>::type’ {aka ‘const yyy’})
- C# app.config with multiple values with a delimited value
- Taking objects from list only if not empty
- Linq query from 3 tables using Joiner table
- Marking possible duplicates as distinct
- Editing a record by accessing it
- Getting last two rows in a text file
- LINQ select: want to pass collection into CONTAINS
- SQL to select rows that match a URL with wildcard