score:16
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 foreach
correctly.
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.
score:0
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());
Source: stackoverflow.com
Related Query
- 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
More Query from same tag
- NHibernate.Linq, WCF RIA Services, weird error
- How to group and count the number of occurence of item in comma delimited string
- MVC linq query handle situation where sequence contains no elements
- Reaching connected tables with constraints same way linq/entity does it with .ToList(), just in clean Sql
- Linq Except ignoring Custom comparer?
- How to do CRUD on datagridview with an anonymous type datasource
- LINQ filter fields that starts with a particular integer
- Search through Where clause in LINQ, using dynamic properties
- Negating a method call in an Expression tree
- How this linq execute?
- Flattening Complex LINQ to SQL
- What is the Maximum Number of Records i can insert through InserAllOnSubmit() In Linq to Sql
- c# Replace string in a property of a class using linq not working
- Accessing XML elements with LINQ using VB.net (not C#)
- linq to entities retrieve database table row value and display as label.Text C# .net
- Linq query to remove the first entry with value > 0 in dictionary, when all subsequent values are zero
- How to construct a raw SQL Query in EF Core with dynamic number of parameters
- How to round up time difference to the nearest seconds with string format?
- Explain this LINQ code?
- Scope of Linq Expressions defined in a loop - At issue: closing over loop variable
- Translate SQL query to LINQ (subqueries)
- Single User Application File/Data Storage Recommendations
- Entity Framework: how do I run a stored procedure and return a value?
- How to do Count(), Group with QueryExpression/ObservableCollection Result with ASP.NET?
- Compose LINQ-to-SQL predicates into a single predicate
- Sql Server Stored procedure returns -1
- Get values from stored procedure to class in c# using linqToSql
- How do I convert multiple inner joins in SQL to LINQ?
- LINQ query select entity and its collection query
- Cleaner looking way to do case in linq