English 中文(简体)
我如何利用Mlambda的经营者来改变这一lin问?
原标题:How do I modify this linq query using the lambda operator?
  • 时间:2009-10-02 20:46:13
  •  标签:

我有以下小 the:

 var files = (from d in new DirectoryInfo(@"c:program files").GetDirectories()
                         where d.GetFiles().Count() > 10
                         where d.GetFiles().Count() < 100
                         select d
             );

然而,正如你上文所看到的那样,我两次叫D.GetFiles().Count(......)是“=>”的概念,操作者是手里的。

利用吉德哈那的辛子,我为什么可以这样做:

var files = (from d in new DirectoryInfo(@"c:program files").GetDirectories()
                         where(x => x.GetFiles().Count() > 10) &&
                         (x.GetFiles().Count() < 100))
                         select d
             );

我知道上述情况是站不住脚的,因为我只能与Cunt(Count)(d.GetFiles())做我的原始询问,但我很想知道的是,与关键词相比,在使用这些关键词之间有何区别。

最佳回答

使用<代码>let在查询中进行分配,如上所示。

var files = (from d in new DirectoryInfo(@"c:program files").GetDirectories()
   let f = d.GetFiles().Count()
   where f > 10
   where f < 100
   select d
);

正如Reed Copsey指出的那样,您可以使用&&合并这两个where子句。

另外,由于<代码>GetFiles>,你可以使用<代码>Length<>/code>财产而不是<编码>Count方法。

  var files = (from d in new DirectoryInfo(@"c:program files").GetDirectories()
     let f = d.GetFiles().Length
     where f > 10 && f < 100
     select d
  );
问题回答

Lambda的言论将无助于这里,但关键词可能会......

var files = from d in new DirectoryInfo(@"c:program files").GetDirectories()
                  let c = d.GetFiles().Count()
                  where c > 10 && c < 100
                  select d;

涉及“删除”关键词的答案可能是你想要使用的东西。

我提供了这一替代答案,以表明如何利用“=”来完成同样的事情。

第一,确保您为<代码>使用系统:Linq;。

var files = new DirectoryInfo(@"c:program files").GetDirectories().Where(d =>
  {
    int c = d.GetFiles().Count();
    return c > 10 && c < 100;
  });

你们可以看到,在这个情况下,玛尔布达语是一种“更好的”解决办法,只是不同的。

它想用lam语来看待:

var files = new DirectoryInfo(@"c:program files").GetDirectories().
    Where(x => (x.GetFiles().Count() > 10) && (x.GetFiles().Count() < 100));

页: 1 玛尔巴达的言论无助于解决这一问题。

EDIT:除了表面上说,它实际上效率不高,更喜欢在其他答复中提出的<><><>>t><>>>>>>> /strong” 解决办法,这一答案意在表明它喜欢用“lambda”表示的文字撰写。

www.un.org/Depts/DGACM/index_spanish.htm 他看着“Paul Williams”的回答,表明如何以比照的更佳和更符合“let 解决办法的精度的方式,用手提语表达。





相关问题
热门标签