Linq:如何在查询语法中编写 Any ?

IEnumerable<Gruppe> cand = (IEnumerable<Gruppe>)populations.Where(
                            x => !x.Attributes.Any(
                                 y => y.GetType() == typeof(Arbeit)
                              )
                          );

我想知道如何用查询语法编写上述内容,但由于 Any 方法而被绊倒。


弑天下
浏览 89回答 3
3回答

慕沐林林

查询语法中没有与 Any 等效的内容。所以你能做的最好的事情是:IEnumerable<Gruppe> cand =&nbsp; &nbsp;from population in populations&nbsp; &nbsp;where !population.Attributes.Any(y => y.GetType() == typeof(Arbeit))&nbsp; &nbsp;select population(我假设没有必要强制转换为 IEnumerable<Gruppe>。如果该假设错误,您需要将其添加回来。)

holdtom

我喜欢斯维克的回答。另一种说法是这样的:IEnumerable<Gruppe> cand =&nbsp;&nbsp; &nbsp; from population in populations&nbsp; &nbsp; where ( from attribute in population.Attributes&nbsp; &nbsp; &nbsp; &nbsp; where attribute.GetType() == typeof(Arbeit)&nbsp; &nbsp; &nbsp; &nbsp; select true).Any() == false&nbsp; &nbsp; select population

海绵宝宝撒

Any 可以重构,但您仍然需要 Count。var cand = from population in populations&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;let attributeCount = population.Attributes.Count&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;let validAttributeCount = (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;from attribute in population.Attributes&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;where attribute.GetType() != typeof(Arbeit)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;select attribute&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;).Count()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;where attributeCount == validAttributeCount&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;select population;
打开App,查看更多内容
随时随地看视频慕课网APP