I managed to make a selection from the complex structure of the object, but only with the help of foreach, how can I avoid this foreach and solve my problem, just using LINQ?
You do not use LINQ for this purpose. You are using
LINQ is for querying data. A
foreach loop is about producing a side effect repeatedly. The body of your
foreach is mutating a property of an object; that's an update and not a query, so you are doing it right. Using LINQ for that is wrong; don't do it.
Answers that say to, for instance, use
ToList to force iteration of a query with a side effect are extremely bad style and result in code which is inefficient, hard to understand, hard to maintain, and works against the purpose of the query operators. NEVER abuse LINQ like that. We have a construct built into the language that means "perform this operation once per collection element", and it is called
foreach. Use it.
Are you looking for something like this?
product.ForEach(item => item.Strucutre.CheckList.Checks = item.Strucutre.CheckList.Checks.Where(w => numbers.Contains(w.NumberAsInt)).Select(w => w).ToList());
- LINQ C# complex nesting structure
- Suggestions for designing complex LINQ code
- LINQ Source Code Available
- creating Linq to sqlite dbml from DbLinq source code
- source code for LINQ 101 samples
- Single linq query for complex data structure : Dict<int, Dict<str, _class>>
- Create a tree structure in linq with a single list source with parent - child as strings of an object
- Complex SQL to LINQ to populate Grid (Count items year by month) (MVC, EF Code First)
- c# Linq or code to extract groups from a single list of source data
- LINQ query code for complex merging of data
- Entity Framework Code First - The entity or complex type cannot be constructed in a LINQ to Entities query
- Convert string to int in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Linq code to select one item
- How are people unit testing code that uses Linq to SQL
- Linq Aggregate complex types into a string
- VB.NET LINQ Query: Getting The Sum of All Values For A Specific Structure Member
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Syntax to execute code block inside Linq query?
- LINQKit: nesting an ExpandableQuery in LINQ to Entities
- Complex Linq Grouping
- System.ArgumentException: Complex DataBinding accepts as a data source either an IList or an IListSource
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Best open source LINQ provider
- Linq slowness materializing complex queries
- LINQ Where clause with Contains where the list has complex object
- Is there a good source that gives an overview of linq optimizations?
- Does this LINQ code perform multiple lookups on the original data?
- The entity or complex type ... cannot be constructed in a LINQ to Entities query
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- Dynamically projecting with LINQ
- Parse XML in List<CustomClass> with Linq query
- VB.NET LINQ query Where with AndAlso and Or
- Linq query to return top two results when there is some conflict, is not working?
- LINQ: Select a collection?
- Complex LINQ to XML query assistance
- How to construct a raw SQL Query in EF Core with dynamic number of parameters
- How to get attributes from xml/scxml
- Iterate starting from a preset position and continue iterating after end is reached
- Dynamic query building with entity framework core - Build a query "by steps"
- How to convert this SQL query to LINQ or Lambda expression?
- Why does ReSharper tell me "implicitly captured closure"?
- IQueryable command timeout
- LINQ to find array indexes of a value
- Mixing ref and params keyword is not possible?
- Get X random elements from table in database using Linq or lambda in C#
- Split String with Custom conditions and delimeters
- ImmutableArray<> behaves differently than Array<> for nested Select with index
- The long expression could not be translated
- How can I incorporate an Expression<Func<T, TPropertyType>> into my IQueryable?