C# 在IEnumerable上筛选。哪里
我做了一个函数来生成奇数:C# 在IEnumerable上筛选。哪里,c#,generator,ienumerable,C#,Generator,Ienumerable,我做了一个函数来生成奇数: static IEnumerable<int> OddNumbers() { int n = 1; while (true) yield return 1 + 2 * (n++ - 1); } 但是有人告诉我 The type arguments for method `System.Linq.Enumerable.Where<TSource>( this System.Collections.Generic.IEnume
static IEnumerable<int> OddNumbers()
{
int n = 1;
while (true)
yield return 1 + 2 * (n++ - 1);
}
但是有人告诉我
The type arguments for method `System.Linq.Enumerable.Where<TSource>(
this System.Collections.Generic.IEnumerable<TSource>,
System.Func<TSource,bool>)' cannot be inferred from the usage.
Try specifying the type arguments explicitly`
方法'System.Linq.Enumerable.Where'的类型参数(
此System.Collections.Generic.IEnumerable,
无法从用法推断System.Func“”。
尝试显式指定类型参数`
据我所知,不需要直接访问枚举器,只需使用Linq即可完成,如下所示
var FilteredNumbers = OddNumbers().Where(x => x % factor != 0);
据我所知,不需要直接访问枚举器,只需使用Linq即可,如下所示
var FilteredNumbers = OddNumbers().Where(x => x % factor != 0);
您可以使用Linq:
您可以使用Linq:
因为您需要它来生成幸运数字序列,所以这里有一些代码是我用迭代器编写的。请注意,这样做效率很低,但希望您这样做只是为了好玩或学习
static IEnumerable<int> LuckyNumbers(IEnumerable<int> all = null, int n = 2, int step = 0) {
if (step == 0) {
all = Enumerable.Range(1, int.MaxValue); // start with all numbers
yield return 1;
step++;
}
// apply a filter for current "n" (starting with 2)
var filtered = Filtered(all, n);
// get next item from the sequence (skip items first, because this sequence represents whole lucky number sequence, starting from 1)
var current = filtered.Skip(step).First();
yield return current;
step++;
// now recursive call back into LuckyNumber
foreach (var other in LuckyNumbers(filtered, current, step)) {
yield return other;
}
}
static IEnumerable<int> Filtered(IEnumerable<int> previous, int n) {
// filter out each n-th item
return previous.Where((x, i) => (i + 1)%n != 0);
}
因为您需要它来生成幸运数字序列,所以这里有一些代码是我用迭代器编写的。请注意,这样做效率很低,但希望您这样做只是为了好玩或学习
static IEnumerable<int> LuckyNumbers(IEnumerable<int> all = null, int n = 2, int step = 0) {
if (step == 0) {
all = Enumerable.Range(1, int.MaxValue); // start with all numbers
yield return 1;
step++;
}
// apply a filter for current "n" (starting with 2)
var filtered = Filtered(all, n);
// get next item from the sequence (skip items first, because this sequence represents whole lucky number sequence, starting from 1)
var current = filtered.Skip(step).First();
yield return current;
step++;
// now recursive call back into LuckyNumber
foreach (var other in LuckyNumbers(filtered, current, step)) {
yield return other;
}
}
static IEnumerable<int> Filtered(IEnumerable<int> previous, int n) {
// filter out each n-th item
return previous.Where((x, i) => (i + 1)%n != 0);
}
将
oddNumbers
声明为:IEnumerable oddNumbers=oddNumbers()代码>。然后在中使用
为枚举数使用另一个变量。oddNumbers
是一个IEnumerator
,但其中
需要一个IEnumerable
。您可以使用OddNumbers()。其中(x=>x%因子!=0)
。呃。。。所有这些操作似乎都是无限的……oddNumbers
是一个IEnumerator,而不是IEnumerable。我不知道你怎么能在上面执行Where
。如果你能告诉我们你试图解决的整个问题,那就太好了。声明oddNumbers
为:IEnumerable oddNumbers=oddNumbers()代码>。然后在中使用
为枚举数使用另一个变量。oddNumbers
是一个IEnumerator
,但其中
需要一个IEnumerable
。您可以使用OddNumbers()。其中(x=>x%因子!=0)
。呃。。。所有这些操作似乎都是无限的……oddNumbers
是一个IEnumerator,而不是IEnumerable。我不知道你怎么能在它上面执行Where
。如果你告诉我们你试图解决的整个问题,那就好了。但是,请确保对可枚举项应用任何保护措施,例如Take(),以防止在以后迭代整个集合时出现无限循环:foreach,.ToList()。等…我反复过滤收集,并希望下一个过滤器使用前一个过滤器的输出。是否有办法就地筛选集合并存储结果?准备好再次筛选了吗?但是,请确保对可枚举项应用任何保护,例如Take(),以防止以后在整个集合中迭代时出现无限循环:foreach、.ToList()。等…我反复过滤收集,并希望下一个过滤器使用前一个过滤器的输出。是否有办法就地筛选集合并存储结果?准备好再次筛选了吗?我正在重复筛选集合,希望下一个筛选器使用上一个筛选器的输出。是否有办法就地筛选集合并存储结果?准备好再次筛选了吗?您可以将筛选器堆叠起来:var result=OddNumbers()。其中(item=>item%factor!=0)。其中(item=>item<100000)。其中(item=>(item*item)%5==1)代码>只需在
所在的位置放置尽可能多的required@theonlygusti:生成器会返回无限多的项,因此请注意一些简单的解决方案,如.ToList()
。将TakeWhile()
或Take()
放在之前。ToList()
我从不想要列表。我不想把它作为一个列表。但是谢谢你。@theonlygusti:我明白了;我刚刚读过“我如何浏览和筛选此列表”。在有限多个项目是可以的(有一些限制,例如,您不能放置OrderBy
,ToList()
等),无限多个过滤器是不可以的(除非它们不是任意过滤器)。我重复过滤集合,并希望下一个过滤器使用上一个过滤器的输出。是否有办法就地筛选集合并存储结果?准备好再次筛选了吗?您可以将筛选器堆叠起来:var result=OddNumbers()。其中(item=>item%factor!=0)。其中(item=>item<100000)。其中(item=>(item*item)%5==1)代码>只需在
所在的位置放置尽可能多的required@theonlygusti:生成器会返回无限多的项,因此请注意一些简单的解决方案,如.ToList()
。将TakeWhile()
或Take()
放在之前。ToList()
我从不想要列表。我不想把它作为一个列表。但是谢谢你。@theonlygusti:我明白了;我刚刚读过“我如何浏览和筛选此列表”。在有限多个项目是可以的(有一些限制,例如你不能把OrderBy
,ToList()
等等),无限多个过滤器不是(除非它们不是任意过滤器)。你告诉我“这与X的可除性无关(因此,与X%因子无关)”,但我在你自己的回答中看到:“.Where((X,I)=>(I+1)%n!=0;”@在我的答案中,我不是除掉x,而是除掉I(序列中项目的索引,而不是数字本身)。我在问题中犯了一个错误。你告诉我“这与X的可除性无关(因此,与X%因子无关)”,但我在你自己的答案中看到:“.Where((X,I)=>(I+1)%n!=0)”@theonlygusti在我的答案中,我不是除以X,而是除以I(序列中项目的索引,而不是数字本身)。我在最新的解决方案中这样做的。我在这个问题上犯了一个错误。
foreach (int item in result) {
//TODO: put relevant code here; do not forget to break the loop
}
static IEnumerable<int> LuckyNumbers(IEnumerable<int> all = null, int n = 2, int step = 0) {
if (step == 0) {
all = Enumerable.Range(1, int.MaxValue); // start with all numbers
yield return 1;
step++;
}
// apply a filter for current "n" (starting with 2)
var filtered = Filtered(all, n);
// get next item from the sequence (skip items first, because this sequence represents whole lucky number sequence, starting from 1)
var current = filtered.Skip(step).First();
yield return current;
step++;
// now recursive call back into LuckyNumber
foreach (var other in LuckyNumbers(filtered, current, step)) {
yield return other;
}
}
static IEnumerable<int> Filtered(IEnumerable<int> previous, int n) {
// filter out each n-th item
return previous.Where((x, i) => (i + 1)%n != 0);
}
foreach (var next in LuckyNumbers().Take(10)) {
Console.WriteLine(next);
}