I have the following linq query:
var files = (from d in new DirectoryInfo(@"c:\program files").GetDirectories()
where d.GetFiles().Count() > 10
where d.GetFiles().Count() < 100
select d
);
However, as you can see above, I am calling d.GetFiles().Count() twice, is this where the concept of the => operator comes in handy.
Using the syntax from jdehaan, why can’t I do it directly like this:
var files = (from d in new DirectoryInfo(@"c:\program files").GetDirectories()
where(x => x.GetFiles().Count() > 10) &&
(x.GetFiles().Count() < 100))
select d
);
I know the above is stupid because I could just do my original query with d.GetFiles().Count(), but I am curious on the difference between using Where as opposed to the keyword where.
Use
letto assign within the query, like thisAs Reed Copsey points out you can merge the two
whereclauses using&&.Also, since
GetFilesreturns an array you can use theLengthproperty instead of theCountmethod.