score:2
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 )
} );
Source: stackoverflow.com
Related Query
- Building up an object in linq - adding properties that depend on other properties
- How can I set properties on all items from a linq query with values from another object that is also pulled from a query?
- Dynamic "Not" by parameter in LINQ (Or any other code for that matter)
- Building which properties to select with LINQ To Object
- Linq : Object property based on other properties of same object
- How are people unit testing code that uses Linq to SQL
- Iterate through properties and values of an object returned via a linq query on a domain model
- Using LINQ for an object that only has GetEnumerator()
- LINQ query expressions that operate on types (monads?) other than IEnumerable<T> -- Possible uses?
- LINQ - Returning value of a property of an object that is not null
- Is there a good source that gives an overview of linq optimizations?
- LINQ - SelectMany from multiple properties in same object
- Replicating LINQ to Entities behavior in unit tests that use LINQ to Object
- Linq to entities - SQL Query - Where list contains object with 2 properties (or more)
- Determine, that sequence contains other sequence, in same order, with using LINQ
- Join in LINQ that avoids explicitly naming properties in "new {}"?
- Linq object properties disappear after serialization
- LINQ Source Code Available
- How can I switch that code to use LINQ
- C# Create object with dynamic properties : LINQ select List<object> values by property names array
- How does this linq code that splits a sequence work?
- Linq with where clause in many-to-many EF Code First object
- How to copy all properties of an object to another in LINQ
- linq - how do you do a query for items in one query source that are not in another one?
- EF6 Query Criteria using object properties that aren't null
- Combine object properties into a list with LINQ
- Apply a linq expression of an object to a list of items where that object is a propery of an item (Wrapper)
- Adding SQL comment to Linq generated query so that it is visible in SQL profiler
- How do i prevent Linq from adding a GUID so that SQL server can do it?
- Custom jQuery Selector That Returns Selected Element Properties Like LINQ
More Query from same tag
- Entity Framework, Dynamic requested entity
- Showing data on Bar graph using Linq and Mvc
- .NET Core: How to merge nested one-to-many relations in dto
- How to convert IQueryable<IList<AnimeTitle>> to IQueryable<AnimeTitle>
- Combining 2 records that have duplicate ID columns in Datatable with for loop
- Calculating Count for IEnumerable (Non Generic)
- What's the linq version of a multiple field group by count?
- Updating node contents from another file in c#?
- Does MongoDb C# driver works with LINQ and dynamic documents?
- Linq-to-Sql SubmitChanges not updating fields ... why?
- LightSwitch PreprocessQuery not filtering
- Ugly LINQ statement, a better way?
- Getting all objects from all IEnumerables within all IEnumerables
- Linq: the query with Grouping, having and Max
- How to write this example in a more succinct way in LINQ?
- linq query for sql xml column
- Convert loop to LINQ
- Linq to entity - Call user defined method from query
- Using LINQ/Lambdas to copy a String[] into a List<String>?
- ADO.NET EF and LINQ pagging records & translation to SQL
- C# Entity Framework 4 Common Language Runtime detected an invalid program error?
- How to add a where clause inside select query
- Linq: How to query items from a collection until the sum reaches a certain value
- Entity Framework- Doing a join when there won't always be a match
- Linq join result to List
- A Problem in Linq expression
- Linq to iterate through a collection and add another collection to its member
- linq query using joins and aggregate functions
- EF Core updating foreign key is not updating the navigation property
- One LINQ query to get counts from several entities when using entity framework core