score:11
Assuming the shared properties implement equality etc properly, you might want to do something like:
// In an extensions class somewhere. This should really be in the standard lib.
// We need it for the sake of type inference.
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> items)
{
return new HashSet<T>(items);
}
var shared = foos.Select(foo => new { foo.SharedProperty1, foo.SharedProperty2 })
.ToHashSet();
var query = bars.Where(bar => shared.Contains(new { bar.SharedProperty1,
bar.SharedProperty2 }));
The other options using joins will certainly work - but I find this clearer for the expressing the idea that you only want to look at each bar
entry once, even if there are several foo
items with that property.
score:3
Sounds like you need a join:
var fooList = new List<foo>();
var barList = new List<bar>();
// fill the lists
var results = from f in fooList
join b in barList on
new { f.commonPropery1, f.commonProperty2 }
equals
new { b.commonProperty1, commonProperty2 = b.someOtherProp }
select new { f, b };
score:1
Any
should work if it's irrelevant how many foo's match:
var selectedBars = bars.Where(bar =>
foos.Any(foo => foo.Property1 == bar.Property1
&& foo.Property2 == bar.Property2));
If all foos should match use All
var selectedBars = bars.Where(bar =>
foos.All(foo => foo.Property1 == bar.Property1
&& foo.Property2 == bar.Property2));
If the list of foos is large use a HashSet
as pointed out in John Skeets answer.
Source: stackoverflow.com
Related Articles
- C# LINQ comparing list values
- Comparing a list of CSV values in Linq to match those from second list with any value in first list
- C# Linq Comparing List Values using multiple fields and return item which doesn't satisfy the condition
- Comparing a list of values against the property of an object (list) using LINQ
- c# Linq or code to extract groups from a single list of source data
- 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
- LINQ Get Distinct values and fill LIST
- Select distinct values from a list using LINQ in C#
- How to get a list of the grouped values in linq groupby?
- Comparing 2 objects and retrieve a list of fields with different values
- Linq query to get the distinct values in a list
- Convert a list to a dictionary and sum up values using linq
- Find the Second Max in a list of values using linq c#
- Group by key and send values into list using LINQ
- LINQ - get total count of values from nested list
- c# linq GroupBy on the values of a List within a List
- Assign values from one list to another using LINQ
- LINQ Source Code Available
- Use LINQ in order to select a list by matched sublist values in C#
- EF Code First comparing null values generates strange query
- Linq Query to Compare with List of string values
- Linq List contains specific values
- How to add list of missing values in c# list using Linq
- LINQ query returns old results when source list is re-initialized
- LINQ to Object comparing two lists of integer for different values
- Linq List comparing and exclusion
- C# - Linq optimize code with List and Where clause
- MongoDB c# Driver - Perform a LINQ "Any" in a Serialized Dictionary
- Combine similar LINQ queries
- LINQ to SQL doesn't exist in my visual studio 2012
- Check that all items of IEnumerable<T?> has the same value using LINQ
- Using a foreach loop on LINQ query for searching
- How to total quantity based on date range from another table?
- How to use the IEqualityComparer
- DateTime Conversion in Entity Framework
- how to write code to find element in directory in Linq
- XML LINQ: How to use select with where?
- SubSonic3 Linq query generates "IS NOT NULL" instead of "IS NULL"
- How to use filter result with list integer in IQueryable
- Is it possible to extend the Query-Keywords in C# / LINQ?
- Union with LINQ to XML
- How get count of each distinct item in generic List in C#
- Construct an IOrderedQueryable<T> from string
- Filter IEnumerable Collection using Linq on ASP.NET Web API 2
- Asp.Net Identity 2.0 Return Roles (By NAME) with a LIST of Users
- LINQ Select Question
- Default value for linq select item if query didn't return anything