score:1
Let's do this step by step and then we can merge operations where necessary.
I want to return a list of WorksiteGroup with the full structure above where IsDiscontinued is false
source.Where(e => !e.IsDiscontinued);
and have an ActiveState where StateAbbrev matches any of the filter criteria (states[])
now let's take the previous pipeline and chain this criterion into it.
source.Where(e => !e.IsDiscontinued)
.Where(e => e.ActiveStates.Any(a => states.Contains(a.StateAbbrev)))
and IsDiscountApplied is true for that state.
source.Where(e => !e.IsDiscontinued)
.Where(e => e.ActiveStates.Any(s => states.Contains(s.StateAbbrev) && s.IsDiscountApplied));
for efficiency let's swap the Contains
call to be after s.IsDiscountApplied
e.g.
source.Where(e => !e.IsDiscontinued)
.Where(e => e.ActiveStates.Any(s => s.IsDiscountApplied && states.Contains(s.StateAbbrev)));
score:1
You can try this using Linq:
string[] states = new string[] { "abbrev1", "abbrev2" };
var list = new List<WorksiteGroup>();
var item = new WorksiteGroup();
item.Name = "Test1";
item.IsDiscontinued = false;
var subitem = new WorksiteGroupState();
subitem.IsDiscountApplied = true;
subitem.StateAbbrev = "abbrev1";
item.ActiveStates.Add(subitem);
list.Add(item);
item = new WorksiteGroup();
item.Name = "Test2";
item.IsDiscontinued = true;
subitem = new WorksiteGroupState();
subitem.IsDiscountApplied = true;
subitem.StateAbbrev = "abbrev1";
item.ActiveStates.Add(subitem);
list.Add(item);
var result = list.Where(wg => wg.IsDiscontinued == false
&& wg.ActiveStates.Where(state => state.IsDiscountApplied == true
&& states.Contains(state.StateAbbrev)).Any());
foreach ( var value in result )
Console.WriteLine(value.Name);
Console.ReadKey();
You can play with items and add more to see results.
score:0
sudo-code but would something like below work, im sure you could do this is one line but
var worksiteGroup = Populate();
var filteredWorkSiteGroup = worksiteGroup .Where(x=>x.IsDiscontinued == false);
filteredWorkSiteGroup.ActiveStates = filteredWorkSiteGroup.ActiveStates
.Where(x=> states.Contains(x.StateAbbrev)
&& x.IsDiscountApplied == true);
Source: stackoverflow.com
Related Articles
- C# LINQ Filter list of complex objects by sub-list using a list of values
- List of class objects - Filter class objects with same values using LINQ
- Filter a list of objects using LINQ
- I need to compare list with Datatable and filter values using Linq
- C# Using LINQ to filter each Object with Max Value in List of Objects
- Filter linq query results using values from a list
- Filter a list of objects using a property that is another list. Using linq
- Filter list of entity objects by string child object property using Linq Lambda
- Using a Linq query to select objects where any value of a property matches values from a list
- Using Linq to Filter a List of Objects based on a Condition
- Using linq to compare object values works for single object but not a list of objects
- using dynamic linq to filter a list of objects
- Using LINQ how do you filter a list of strings for values that match the pattern "q1, q2" etc.?
- Building an string from properties values of objects in a list using linq
- LINQ Filter values on list values using contain
- Searching if value exists in a list of objects using Linq
- Using Linq to group a list of objects into a new grouped list of list of objects
- LINQ query to return distinct field values from list of objects
- How to select values within a provided index range from a List using LINQ
- Get indexes of all matching values from list using Linq
- Convert dictionary values to list using linq
- Using LINQ to group a list of objects
- Select distinct values from a list using LINQ in C#
- Simplest way to filter value from generic List in C# using LINQ
- Change the property of objects in a List using LINQ
- Find child objects in list of parent objects using LINQ
- Sort a list and all its nested objects using LINQ
- Convert a list to a dictionary and sum up values using linq
- Using LINQ to merge a list of objects
- Find the Second Max in a list of values using linq c#
- Using LINQ for JOINs and Sum
- Cannot implicitly convert type system linq IQueryable to system collections generic List
- Linq group by query
- How do I write this block of code in LINQ?
- Is there any way to make Code Contracts work with LINQ?
- emacs F# mode REPL :: using Linq
- Shortcut LINQ toentity query for the following code in MVC 3
- Linq - Join Confusion
- Ternary operator in LINQ query is not working as expected
- LINQ Except using custom Comparer
- DbSortClause expressions must have a type that is order comparable. Parameter name: key
- Flatten jagged array in C#
- Look for words in strings with LINQ
- Covert SQL to Linq Query
- LINQ Entity Framework - The object cannot be deleted because it was not found in the ObjectStateManager
- C# Linq Convert Object Property to Array
- Selecting specific elements from IGrouping using LINQ
- LINQ Intersect not returning items
- Linq - How to take the result for a query in Linq and add it to an Array
- Moving SP-Linq queries from test site to production site