[英]Return List<object> from IEnumerable C#
I have a method: 我有一个方法:
public List<AxeResult> LoadAxes(string kilometriczone, string axe)
{
IEnumerable<AxeResult> allAxes = PullAxes();
var findAxes = allAxes.Select(a => a.KilometricZone.StartsWith(kilometriczone) || a.KilometricZone.StartsWith(axe));
return findAxes.Cast<AxeResult>().ToList();
}
I have this error: 我有这个错误:
IEnumerable<bool>
does not contain a definition forToList
and the best extension method overloadEnumerable.ToList<AxeResult>
(IEnumerable<AxeResult>
) requires a receptor typeIEnumerable<AxeResult>
IEnumerable<bool>
不包含ToList
的定义和最佳扩展方法重载Enumerable.ToList<AxeResult>
(IEnumerable<AxeResult>
)需要一个受体类型IEnumerable<AxeResult>
I want to return a List of AxeResult after the search operation. 我想在搜索操作后返回一个AxeResult列表。
What you want is to filter the collection. 你想要的是过滤集合。 That's what
Enumerable.Where
is for: 这就是
Enumerable.Where
的用途:
public List<AxeResult> LoadAxes(string kilometriczone, string axe)
{
return PullAxes()
.Where(a => a.KilometricZone.StartsWith(kilometriczone) ||
a.KilometricZone.StartsWith(axe))
.ToList();
}
Some explanation in addition to the given answers: 除了给出的答案之外还有一些解释:
Where() method acts like a filter and returns a subset of the same set Where()方法充当过滤器并返回同一组的子集
Select() method makes projection and returns new set Select()方法进行投影并返回新的集合
There is comprehensive explanation about the diffrence between .Select() and Where() methods here . 还有约。选择差异性之探源()和凡全面的解释()方法在这里 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.