score:5
it is also possible to add an inline if in the where clause
lstitem.itemssource =
(from item in items
where (test == "s" ? item.property1 == "somevalue" : item.property2 == "someothervalue")
select item);
score:2
well, you could start by boiling the expression down to:
func<items, bool> expr;
if(type== "s")
{
expr = (item => item.property1 == "somevalue");
}
else
{
expr = (item => item.property2 == "someothervalue");
}
var items = items.where(expr);
of course, the game plan is really to make it all a single statemnet, but this makes it a little more manageable i think :)
jim
score:3
i like:
lstitem.itemssource = items.where(type == "s" ?
item => item.property1 == "somevalue":
item => item.property2 == "someothervalue");
score:4
you can chain your commands within if statements. e.g.:
var items = from item in items
select item;
if(type== "s")
{
items = items.where(item => item.property1 == "somevalue");
}
else
{
items = items.where(item => item.property2 == "someothervalue");
}
or even just write the tidier lambda structure in you orignal code:
if(type== "s")
{
lstitem.itemssource = items.where(item => item.property1 == "somevalue");
}
else
{
lstitem.itemssource = items.where(item.property2 == "someothervalue");
}
Source: stackoverflow.com
Related Query
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- LINQ Source Code Available
- linq - how do you do a query for items in one query source that are not in another one?
- How can I write the following code more elegantly using LINQ query syntax?
- How to dynamic add filters to a LINQ query against an Odata Source in C#
- C# Linq query help removing foreach loops creating cleaner code
- Use a linq query as microsoft local report Data Source (WinForms)
- Determine the source DataContext for a Linq to Sql query
- Refactoring LINQ query
- LINQ query returns old results when source list is re-initialized
- How to get SQL query into LINQ form in C# code
- How can I code a Linq query to do an upward Include?
- creating Linq to sqlite dbml from DbLinq source code
- Identify source of linq to sql query
- NHibernate LINQ query performance, which code fragment is better?
- Linq sub query when using a repository pattern with EF code first
- Using LINQ query result for data source for GridControl c#
- convert linq to object query to sql query (no linq to sql code or datacontext)
- Getting 'Data source is an invalid type' when binding Linq query to Gridview
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- Converting foreach loop to LINQ query breaks code
- C# code for equivalent LINQ query
- How do I determine the source of unpredictable LINQ query results?
- linq null refactoring code
- How to Select top (5) contributors group by Business type code in c# linq query
- How can I code numerous MIN functions into one LINQ to DataSet query
- Avoiding repeating code with Linq query + optional params
- Unreachable expression code in LINQ query
- Linq code refactoring
More Query from same tag
- How do you tell at run time if an IEnumerable<T> is deferred?
- LINQ with two groupings
- Expression tree to initialize new anonymous object with arbitrary number of properties
- How to use Linq aggregate with single quotes
- Entity Framework 5 will not add Item to Collection, how to fix this?
- Why can I not use OrderBy() in this lambda expression?
- Trying Extract List of Categories with Corresponding Tickets using Linq
- Why does adding a list to another list, using add range, remove the elements from the first list?
- How to map a list of objects on another one with condition
- c# order by distance from coordinates
- Mock same method in a test using Moq with different linq expressions
- How to apply self join in Linq Query?
- select for dynamic result
- How to sort in a linq query?
- Entity Framework Linq foreach performance vs using Select() instead
- Suggestions for designing complex LINQ code
- LINQ - mandatory field and dont care if null
- F# List SelectMany
- sum for nullable field in linq
- How do I query identity data efficiently in ASP.Net Core?
- ORM and SOA in the .NET world
- Loading LINQ results into a ViewModel that has a calculated attribute (column)
- Get attribute value of parent node by Linq to XML
- Updating attributes of multiple input controls based on input value using Jquery LINQ
- Group by NOT having element in common?
- Linq searching list of objects
- How to add an additional filter to this XML
- Linq Custom Sorting
- Lambda expression Linq-to-SQL get from record 5 to 10
- Translation of c# linq to vb - overload resolution failure in intellisense with 'selectmany' statement