正确的Linq where子句

在我的日常生活中,我写了大量的linq,但是主要是简单的陈述。我注意到,当使用where子句时,有许多种写法,据我所知每种方法都有相同的结果。例如;


from x in Collection

  where x.Age == 10

  where x.Name == "Fido"

  where x.Fat == true

  select x;

至少就结果而言似乎与之等效:


from x in Collection

  where x.Age == 10 &&

        x.Name == "Fido" &&

        x.Fat == true

  select x;

那么,除了语法以外,真的有区别吗?如果是这样,首选样式是什么,为什么?


茅侃侃
浏览 268回答 3
3回答

繁华开满天机

第一个将被实施:Collection.Where(x => x.Age == 10)          .Where(x => x.Name == "Fido") // applied to the result of the previous          .Where(x => x.Fat == true)    // applied to the result of the previous相对于更简单的(和远快可能更快):// all in one fell swoopCollection.Where(x => x.Age == 10 && x.Name == "Fido" && x.Fat == true)
打开App,查看更多内容
随时随地看视频慕课网APP