C# MVC LINQ测试库
我正在为依赖注入创建一个测试存储库,下面是我的测试方法C# MVC LINQ测试库,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,我正在为依赖注入创建一个测试存储库,下面是我的测试方法 private List<object> records; public IList<T> GetFiltered<T>(Expression<Func<T, bool>> action = null) where T : class { return ((List<T>)records).Where(action).ToList(); } 私有列表记录;
private List<object> records;
public IList<T> GetFiltered<T>(Expression<Func<T, bool>> action = null) where T : class
{
return ((List<T>)records).Where(action).ToList();
}
私有列表记录;
公共IList GetFiltered(表达式action=null),其中T:class
{
return((列表)记录).Where(action.ToList();
}
我基本上希望返回一个过滤后的记录列表,其中“action”条件为true
我得到下面的错误
错误2实例参数:无法从“System.Collections.Generic.List”转换为“System.Linq.IQueryable”
请提供帮助。您需要使用的
IEnumerable
版本需要Func
而不是IQueryable
,它采用表达式
public IList<T> GetFiltered<T>(Func<T, bool> action = null) where T : class
{
return ((List<T>)records).Where(action).ToList();
}
public IList GetFiltered(Func action=null),其中T:class
{
return((列表)记录).Where(action.ToList();
}
另外,List
不能强制转换为List
我的建议是将外部类也设置为通用类,即
public class MyContainer<T>
{
private List<T> records;
public IList<T> GetFiltered(Func<T, bool> action = null) where T : class
{
return records.Where(action).ToList();
}
}
公共类MyContainer
{
私人名单记录;
公共IList GetFiltered(Func action=null),其中T:class
{
返回记录.Where(action.ToList();
}
}
据我所知,您不能将对象的列表
转换为泛型类型
您应该执行类似或i get无法将类型“System.Collections.Generic.List”转换为“System.Collections.Generic.List”