score:35
you are looking for selectmany()
+ distinct()
:
list<someotherclass> ret = myclasslist.selectmany( x => x.someotherclasses)
.distinct()
.tolist();
selectmany()
will flatten the "list of lists" into one list, then you can just pick out the distinct entries in this enumeration instead of using union between individual sub-lists.
in general you will want to avoid side effects with linq, your original approach is kind of abusing this my modifying ret
which is not part of the query.
tolist()
is required since each standard query operator returns a new enumeration and does not modify the existing enumeration, hence you have to convert the final enumeration result back to a list. the cost of tolist()
is a full iteration of the enumeration which, in most cases, is negligible. of course if your class could use an ienumerable<someotherclass>
instead, you do not have to convert to a list at all.
score:0
you should have a look at selectmany. something like this should generate your "flat" list:
myclasslist.selectmany(b => b.someotherclasses)
it will return a ienumerable<someotherclass>
which you can filter/process further.
Source: stackoverflow.com
Related Query
- Union multiple number of lists in C#
- Union 2 lists with additional content change for ID that occurs in both source lists
- UNION from multiple lists
- Check if multiple sub lists are present in a source list
- Intersection of multiple lists with IEnumerable.Intersect()
- Simplest way to form a union of two lists
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- Split a list into multiple lists at increasing sequence broken
- Can you combine multiple lists with LINQ?
- Merge multiple Lists into one List with LINQ
- linq union of two lists
- Remove duplicates while merging lists using Union in LINQ
- Generate all Combinations from Multiple (n) Lists
- How to join unknown number of lists in LINQ
- Union A List of Lists Using Linq
- Does this LINQ code perform multiple lookups on the original data?
- Align multiple sorted lists
- Linq get values not shared across multiple lists
- Interleaving multiple (more than 2) irregular lists using LINQ
- How to reuse a linq expression for 'Where' when using multiple source tables
- LINQ to SQL, How to merge multiple IQueryable Lists of different subtype?
- LINQ Source Code Available
- multiple orderby in this linq code
- Query multiple lists in one go
- .NET 4 Code Contracts: "requires unproven: source != null"
- How to use multiple OrderBy's to an unspecified number in LINQ?
- LINQ: Count number of true booleans in multiple columns
- Efficient method of merging multiple lists based on item weights
- performance issue with System.Linq when subdividing a list into multiple lists
- LINQ: Separating single list to multiple lists
More Query from same tag
- How to select array index after Where clause using Linq?
- Replace EF LINQ query containing group by with SQL query
- Select student record based on foreach loop
- C# compare sql datetime to null
- Sort multiple list inside List of objects with the same order of the first list
- how to get child element attribute value from xml to linq in c#
- How keep carriage return from parsing XML
- Linq Count() on selected field
- LINQ query syntax
- How do I rotate cols<->rows?
- How do I properly use distinct and compare?
- Get DISTINCT names
- mvc entity framework select case when
- LINQ to Object - How to implement WHERE clause `If at least one of the elements is` for sub-group
- Want to retrieve Array from database
- How to get the first descendant of a node without knowing the name from XML using LINQ
- C# Linq non-vowels
- Is Aggregate fatally flawed because each into clause is executed separately?
- Get the First characters of a column in a table using linq
- Create a hierarchical JSON Http response structure using LinQ
- Comparing foreign keys in entity framework 3.5
- How can I use query syntax from Linq for return random records?
- List of Many-To-Many relationship EF Code-First
- Query Excel where Rows and Columns are reversed
- Automapper nested mappings and grouping
- C# How to loop through an array and find array with most recent date?
- Can't create Func expression for ordering LINQ query
- How to select a list of the latest times per day from a list of dates
- Linq expression multiple left outer join error
- ASP.NET MembershipUserCollection sort by IsApproved, Comment