我想创建一个方法,我可以传递一个Linq表达式作为参数返回一个新的项目列表。
目前我正在这样做( 基于这个答案 ):
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; class Collection<T> { List<T> items = new List<T>(); public Collection<T> Filter(Expression<Func<T, bool>> query) { Collection<T> c = new Collection<T>(); c = items.Where(query); return c; } }'List'不包含'Where'和最好的扩展方法重载定义'Queryable.Where(IQueryable,Expression>)'需要一个接收者类型'IQueryable'
我不确定在这里做什么来解决这个问题。
I am trying to create a method where I can pass a Linq expression as a parameter to return a new list of items.
Currently I am doing this (based off this answer):
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; class Collection<T> { List<T> items = new List<T>(); public Collection<T> Filter(Expression<Func<T, bool>> query) { Collection<T> c = new Collection<T>(); c = items.Where(query); return c; } }'List' does not contain a definition for 'Where' and the best extension method overload 'Queryable.Where(IQueryable, Expression>)' requires a receiver of type 'IQueryable'
I am not exactly sure what to do here to fix this.
最满意答案
List<T>不是IQueryable<T>但IEnumerable<T> 。 对于IEnumerable<T>的Where扩展,您只能传递委托而不是表达式。
我想你想要那样的东西:
public Collection<T> Filter(Func<T, bool> query) // only the lambda, NO expression { Collection<T> c = new Collection<T>(items.Where(query).ToList()); return c; }请注意, Where只返回IEnumerable<T>但不返回Collection<T> 。 因此,您需要将c转换为List<T>并将其传递给Collection<T>的构造函数。
List<T> is not IQueryable<T> but IEnumerable<T>. For the Where extension for IEnumerable<T> you can only pass a delegate not an expression.
I guess you want something like that:
public Collection<T> Filter(Func<T, bool> query) // only the lambda, NO expression { Collection<T> c = new Collection<T>(items.Where(query).ToList()); return c; }Note that Where only returns an IEnumerable<T> but not an Collection<T>. So you need to convert c to a List<T> and pass this to the constructor of Collection<T>.
'列表'不包含'Where'的定义,('List' does not contain a definition for 'Where')我想创建一个方法,我可以传递一个Linq表达式作为参数返回一个新的项目列表。
目前我正在这样做( 基于这个答案 ):
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; class Collection<T> { List<T> items = new List<T>(); public Collection<T> Filter(Expression<Func<T, bool>> query) { Collection<T> c = new Collection<T>(); c = items.Where(query); return c; } }'List'不包含'Where'和最好的扩展方法重载定义'Queryable.Where(IQueryable,Expression>)'需要一个接收者类型'IQueryable'
我不确定在这里做什么来解决这个问题。
I am trying to create a method where I can pass a Linq expression as a parameter to return a new list of items.
Currently I am doing this (based off this answer):
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; class Collection<T> { List<T> items = new List<T>(); public Collection<T> Filter(Expression<Func<T, bool>> query) { Collection<T> c = new Collection<T>(); c = items.Where(query); return c; } }'List' does not contain a definition for 'Where' and the best extension method overload 'Queryable.Where(IQueryable, Expression>)' requires a receiver of type 'IQueryable'
I am not exactly sure what to do here to fix this.
最满意答案
List<T>不是IQueryable<T>但IEnumerable<T> 。 对于IEnumerable<T>的Where扩展,您只能传递委托而不是表达式。
我想你想要那样的东西:
public Collection<T> Filter(Func<T, bool> query) // only the lambda, NO expression { Collection<T> c = new Collection<T>(items.Where(query).ToList()); return c; }请注意, Where只返回IEnumerable<T>但不返回Collection<T> 。 因此,您需要将c转换为List<T>并将其传递给Collection<T>的构造函数。
List<T> is not IQueryable<T> but IEnumerable<T>. For the Where extension for IEnumerable<T> you can only pass a delegate not an expression.
I guess you want something like that:
public Collection<T> Filter(Func<T, bool> query) // only the lambda, NO expression { Collection<T> c = new Collection<T>(items.Where(query).ToList()); return c; }Note that Where only returns an IEnumerable<T> but not an Collection<T>. So you need to convert c to a List<T> and pass this to the constructor of Collection<T>.
发布评论