为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
弑天下 2023-12-17 09:57:34
IEnumerable<Gruppe> cand = (IEnumerable<Gruppe>)populations.Where(                             x => !x.Attributes.Any(                                  y => y.GetType() == typeof(Arbeit)                               )                           );我想知道如何用查询语法编写上述内容,但由于 Any 方法而被绊倒。
查看完整描述

3 回答

?
慕沐林林

TA贡献2016条经验 获得超9个赞

查询语法中没有与 Any 等效的内容。所以你能做的最好的事情是:


IEnumerable<Gruppe> cand =

   from population in populations

   where !population.Attributes.Any(y => y.GetType() == typeof(Arbeit))

   select population

(我假设没有必要强制转换为 IEnumerable<Gruppe>。如果该假设错误,您需要将其添加回来。)


查看完整回答
反对 回复 2023-12-17
?
holdtom

TA贡献1805条经验 获得超10个赞

我喜欢斯维克的回答。


另一种说法是这样的:


IEnumerable<Gruppe> cand = 

    from population in populations

    where ( from attribute in population.Attributes

        where attribute.GetType() == typeof(Arbeit)

        select true).Any() == false

    select population


查看完整回答
反对 回复 2023-12-17
?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

Any 可以重构,但您仍然需要 Count。


var cand = from population in populations

           let attributeCount = population.Attributes.Count

           let validAttributeCount = (

               from attribute in population.Attributes

               where attribute.GetType() != typeof(Arbeit)

               select attribute

           ).Count()

           where attributeCount == validAttributeCount

           select population;


查看完整回答
反对 回复 2023-12-17
  • 3 回答
  • 0 关注
  • 80 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信