Tuesday, April 5, 2011

LINQ - Selecting a property of an object for further use rather than dereferencing it in each place

string output = (from s in abc.longs
                         group s by DateTime.FromFileTimeUtc(s).Minutes < 1
                 .... // so on so forth

The question I have, is I do "DateTime.FromFileTimeUtc(s) like 10 times here, is there any way to do

from s in abc.longs
   t = DateTime.FromFileTimeUtc(s).Minutes
   group by t < 1
From stackoverflow
  • Yes, using the let keyword, which let you declare a symbol you can use later on in the query:

    from s in abc.longs
    let t = DateTime.FromFileTimeUtc(s).Minutes
    group by t < 1
    

    You can find a lot of examples using Google.

    mjsabby : or even Bing :-)

0 comments:

Post a Comment