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: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");
}
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");
Source: stackoverflow.com
Related Articles
- 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
- Getting the 1st value in a subset of data
- What's the best way to construct this LINQ query using orderby and group by?
- Linq Data Source WhereParameters with "\" in value
- Difference in LINQ query results in .NET 3.5 and 4.5
- Nested 3 Tier LINQ Query
- Merge classes based on a property using Linq
- Find keys with min difference in dictionary
- LINQ query to filter page results
- Filter by criteriums in linq query
- How to limit the number of cycles of a loop under some condition?
- retrieving data by LINQ c#
- How to update anonymous objects property using LINQ and Contains
- How to order lists without respecting certain characters?
- Checking if a property in a list of objects is equal for all items
- What is does expression<T> do?
- How to use Linq extension method with CodeFluent Entities template?
- How to combine and update two xml files with LINQ c#
- How can I create a LINQ statement that does a count of a joined table?
- how to check value of an element is not null before assigning using linqtoxml
- Merging multiple rows of a column in a C# list based on other columns