Sounds like you just want:
bool hasSameElements = listA.Intersect(listB).Any();
EDIT: As noted in comments,
Intersect uses lazy evaluation. It defers all execution until the first element is read from the result; at that point it will load all of
listB into a set, and then stream
listA until it finds a result to yield. At that point,
Any() will return
true and so no more work will be done. See my Edulinq post on
Intersect for more information.
- Check for any element that exists in two collections
- Fastest way to check whether a single element in common exists between two enumerables
- Dynamic "Not" by parameter in LINQ (Or any other code for that matter)
- Check if list contains element that contains a string and get that element
- C# Linq to XML check if element exists
- What is the best way to check two List<T> lists for equality in C#
- Any Intersection in Two Collections
- C# check an element exists while using LINQ to XML
- Best way to check if value exists for a key in ILookup<string, string> using linq
- Using linq to determine if any element is ListA exists in ListB?
- Search query using .Contains() for parameters that combine two columns
- Determine if two collections share at least one element
- Is there a way to speed up this code that finds data changes in two XML files?
- linq - how do you do a query for items in one query source that are not in another one?
- Is there any open source software for converting SQL statements to LINQ?
- Union two collections but only for objects with a matching id
- How to check for existence of an element with XElement?
- Data template for two enumerables that are grouped together
- Is there a faster way to check for an XML Element in LINQ to XML?
- LINQ for removing elements that are started with other element from list
- How to do a custom compare for every item in a intersect of two collections
- Need a LINQ code example to link two tables that have no foreign key
- Is there a faster way to check for an XML Element in LINQ to XML, and parse a bool?
- Check if any XML nodes exists with Specific Attribute using LINQ C#
- Does a code that combines single() with yield make any sense?
- LINQ to SQL: How to check if any item in one entity collection exists in another entity collection?
- Is there any preexisting extension for enumerable that makes it NullReferenceException safe?
- EntityFramework Any generates two EXISTS sql statements
- Joining data that exists in two tables and aggregating non joined data
- Select first element that has certain property if there are any duplicates otherwise select first element
More Query from same tag
- How to use where condition
- Return a List with multiple results using LINQ and EF
- Converting for-loop to LINQ
- Using C#, how can we pull attribute values from an XML Schema file and output that onto a CSV file?
- Distinct Count x and Grouping by Date y
- get common prefix of two string
- Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
- Sort results based on occurrences
- Not equal/not contains operation in lambda expressions
- Load most recent related entity record on a list of entities in a single query
- AND OR in a Linq Query
- Why is my code returning a list of the same object 4 times?
- Select inside select (SQL to lambda linq expression)
- Error on LINQ statement with enclosing variable
- Dynamically build a condition on a DataTable using a Dictionary
- LINQ to SQL. Cascade delete sumulation
- how to insert YYMMDD + 4 digits as ID
- Override resolved property type with AutoMapper
- How to write this linq query using Lambda expression
- Linq to Objects - query objects for any non-numeric data
- Importing System.Linq solves "Class cannot be indexed" error
- Construct AndAlso/OrElse LINQ expression with custom method
- OrderBy TimeOfDay not working
- In Linq, what's the difference between .FirstOrDefault and .SingleOrDefault
- Linq not adding data to table, using InsertOnSubmit() & SubmitChanges()
- LINQ List<> Moving Elements up and down
- Join 2 DataTables with many columns
- LINQ query NotSupportedException OrderBy Average of TimeSpan
- Filter nested models on property in last node with NHibernate
- LINQ .Any VS .Exists - What's the difference?