score:7
list.select (x => x.item2 as ienumerable<someobject>)
.aggregate((x,y)=> x.intersect(y))
.tolist();
or, as jeppe stig nielsen suggested (and i think it's much more elegant):
list.select(x => x.item2.asenumerable())
.aggregate(enumerable.intersect)
.tolist();
score:-1
presuming that the class overrides equals
+ gethashcode
or you have a custom iequalitycomparer<someobject>
you can use following query which uses enumerable.all
:
var result = list
.selectmany(t => t.item2) // select all objects
.distinct() // just an optimization since duplicates are guaranteed
.where(obj => list.all(t => t.item2.contains(obj)));
here's my sample data:
var list = new list<tuple<string, list<someobject>>>();
list.add(tuple.create("a", new list<someobject> { new someobject { id = 1 }, new someobject { id = 2 }, new someobject { id = 4 } }));
list.add(tuple.create("b", new list<someobject> { new someobject { id = 1 }, new someobject { id = 2 }, new someobject { id = 3 } }));
list.add(tuple.create("c", new list<someobject> { new someobject { id = 1 }, new someobject { id = 2 }, new someobject { id = 3 } }));
list.add(tuple.create("d", new list<someobject> { new someobject { id = 1 }, new someobject { id = 2 }, new someobject { id = 3 } }));
only the someobjects
with id = 1 or id = 2 are in all lists and that's the result of the query.
score:1
as i understand correctly, you need intersection of many lists:
var results = source.first().item2
foreach (var element in source.skip(1) )
{
results = results.intersect(element.item2)
}
score:1
strongly inspired by juan lopes's beautiful answer, you can define this extension:
static ienumerable<tsource> intersectmany<tsource>(this ienumerable<ienumerable<tsource>> sources)
{
return sources.aggregate(enumerable.intersect);
}
then this works:
var result = list.select(x => x.item2).intersectmany();
it works because ienumerable<out t>
is covariant in t
(c# 4, .net 4.0).
Source: stackoverflow.com
Related Query
- Using LINQ I have a list of lists, how do I select all objects that exist in every list?
- How to select all objects that have a property value in list of values?
- How do I select all objects from a list that have no related objects in another list/model
- Linq return parent objects that have child items matching ALL items in separate list
- How can I select using LINQ for an entry that contains a LIST with more than one row?
- Using Linq to XML, how can I select the elements that have exactly x number of parent elements?
- Extracting a list of objects that exist in another list objects using linq
- Using LINQ in C#, How to retrieve list of events based on specific dates, for example all events that are planned for upcoming 45 days?
- Using Linq to find only records that have all matching values in list of flags?
- Using Linq to do: Compile a list of all lists within a list of objects
- List of objects where the object contains an array - how to use LINQ to search through all objects for a value in that array
- How to select all parent objects into DataContext using single LINQ query?
- Return select objects and only desired subvalues from a list of lists using LINQ
- how can I Select One row where max StartDate in list objects using linq
- How to retrieve a List of Lists of strings that are not null or whitespace from a DataGridView using Linq
- How do i group a list of dynamic objects using linq given that the property names are dynamic?
- How to select values within a provided index range from a List using LINQ
- LINQ Lambda - Find all ID's in one list that don't exist in another list
- Linq - How to select items from a list that contains only items of another list?
- Sort a list and all its nested objects using LINQ
- How to get all elements except the n'th element in a List using Linq
- How to select values in list that are NOT IN a Table using EF Core?
- How to Select All with a One to Many Relationship Using Linq
- LINQ to SQL . how to select all record using .take()
- How to convert list of objects with two fields to array with one of them using LINQ
- Using Linq to group a list of objects that contains primitives data into a new grouped list of objects
- In a C# List how to select all columns + several custom columns, so that in each row all columns are flat (not nested)?
- how do you execute a function on a List of objects using LINQ
- How to join a list of lists in a unique list using Linq
- Using LINQ how to transform a List of Lists using elements index
More Query from same tag
- How to load a datasource with multiple objects at a time in WPF datagrid
- GroupBy and Count using LINQ
- Complex Xml namespaces in c#
- how to execute method or DML in foreach loop as parallel to accelarate execution time?
- Join collection inside an Entity LINQ statement
- Other options than setting IEnumerable to optional
- How to include complex entity fields in a projected entity framework object?
- Using LINQ to compare two sequences for differences
- LINQ Sorting List<T> based on string Name property
- Entity Framework with Include / Joining Issues
- How to populate a Class with Dictionary using LINQ
- object reference not set error in mvc5
- Linq to XML - Get next element of sibling with specific value
- Trying to get a dynamic order by clause in Linq to Entities
- C# algorithm refactor splitting an array into 3 parts?
- fetch email id of those employee who has resigned
- IQueryable doesn't allow ThenBy
- Get previous rows and next rows of linq result for special row
- Delete multiple rows with linq.IQueryable?
- simplifying two queries into one with the same types linq
- Entity & Linq not filtering properly
- Querying an IQuery from a cached list returns null exception
- Convert this Linq query from query syntax to lambda expression
- Query LINQ - Could not find an implementation of the query pattern
- LINQ Query to remove duplicate items in a List based on the value
- Select IN LINQ to entities with inner joins
- EBNF or BNF of LINQ? Associativity of join? Other clauses?
- Deleting a record in a table MVC 4
- How to generate Expression<Func<T, TPaginatedKey>> expression based on a string?
- What does LINQ-to-SQL Table<T>.Attach do?