score:2

Accepted answer

there is not much you can do. another option:

 var data = blgfiles.select( file => 
 {
   csvfile = path.changeextension( file, "csv" );
   return new
   {
      blgfile = file,
      csvfile = csvfile ,
      csvfileexists = file.exists( csvfile  )
   }
 });

it still has the overhead of additional curly braces and requires an explicit return statement, which makes switching between this and the simplified form a bit cumbersome. (unless you use resharper, which does it for you.)

score:0

you can "add properties and create objects where some of the properties depend on the values of previous properties", just there needs to be "previous" properties. the object initializer sets all properties as one statement, and that's the idea of it.

yet you can create an anonymous method in the select's lambda expression that returns the object you want. inside, you can write arbitrary c# code, and this will allow you to have multiple statements, so you will have "previous" ones.

edit: example code of that was already given as answer

score:0

this probably isn't the best example for your question, because really you should be generating two lists of files, and joining them like:

 var blgfiles = directory.getfiles( rootfolder, "*.blg", searchoption.alldirectories );
 var csvfiles = directory.getfiles( rootfolder, "*.csv", searchoption.alldirectories );

 var data = blgfiles.groupjoin(csvfiles,
   k=>path.getfilenamewithoutextension(k),
   v=>path.getfilenamewithoutextension(v),
   (k,g)=>g.select(v=>new {blgfile=k,csvfile=v,csvfileexists=true})
           .defaultifempty(new {blgfile=k,csvfile=path.changeextension(k,"csv"),csvfile=false})
 ).selectmany(g=>g);

or using a single directory lookup:

var files=directory.enumeratefiles(rootfolder, "*.*", searchoption.alldirectories)
        .where(s => s.endswith(".blg") || s.endswith(".csv"));

then group them, and generate your final output. this will likely be the fastest solution as disk i/o is relatively expensive, and doing that part only once will likely outweigh any other considerations.

of course, you can always do this, but i suspect you are trying to avoid it:

 var blgfiles = directory.getfiles( rootfolder, "*.blg", searchoption.alldirectories );

 var data = blgfiles.select( file => new
 {
    blgfile = file,
    csvfile = path.changeextension( file, "csv" ),
    csvfileexists = file.exists(path.changeextension( file, "csv" ))
 } );

but that will generate a lot of extra disk i/o, and calls the changeextension twice. and a simpler version of what you had also works, with same pitfalls:

var data = blgfiles.select( file => new
     {
        blgfile = file,
        csvfile = path.changeextension( file, "csv" )
     } ).select( file => new
     {
        blgfile = file.blgfile,
        csvfile = file.csvfile,
        csvfileexists = file.exists( file.csvfile )
     } );

Related Query

More Query from same tag