You can use
SequenceEqual with additional order:
return list1.OrderBy(x => x).SequenceEqual(list2.OrderBy(x => x));
You should consider using a HashSet, rather than a List. Its a little less expensive:
HashSet<string> firstSet = new HashSet<string>(); HashSet<string> secondSet = new HashSet<string>(); PopulateSets(firstSet, secondSet); //Example of method you might use to populate your hashsets. bool isEqual = firstSet.SetEquals(secondSet);
The SetEquals method ignores duplicate entries and the order of elements in the other parameter.
bool equals = list1.OrderBy(x => x).SequenceEqual(list2.OrderBy(y => y));
I hope this helps ;)
You can use
Contains method like this, this will return true if both lists contains same elements even if the order is different:
bool control = list1.All(x => list2.Contains(x) && list1.Count(a => a == x) == list2.Count(b => b == x));
- How to identify if two List<string> are equal regardless of the order?
- How to union two data tables and order the result
- in C#, how do I order items in a list where the "largest" values are in the middle of the list
- Removing a single item from an enumerable source when the items are equal
- Test whether the elements in two ranges are equal in C# using a lambda and LINQ
- How to check if properties of two objects are equal
- Are these two linq queries of the same performance? And How to implement .Any in linq query?
- XDocument how to get parent of two nodes, that are not on the same level
- How to concat two lists in Linq when the underlying anonymous types are similar?
- linq: how to divide a list into two by the sum of the values and keeping the input order
- How to convert a nested for each loop into Linq when the two lists are of different types
- PowerShell's equivalent of LINQ All, or how can I verify all the items in the collection are equal to specific value?
- How can I retrieve two entities that are associated with a connection entity, only under the name record2roleid?
- Check if two lists are equal
- C# - code to order by a property using the property name as a string
- How do I find the text within a div in the source of a web page using C#
- How are people unit testing code that uses Linq to SQL
- How do I take the Cartesian join of two lists in c#?
- How do I get the differences between two string arrays in C#?
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- LINQ: check whether two list are the same
- How to make a linq Sum return null if the summed values are all null
- How do i get the difference in two lists in C#?
- How to check if all values in an array are equal
- Elegant solution to check if two dates are equal, in Linq (where the second date is NOT a parameter)
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- How to check if all the elements in an array are positive integers?
- How to compare the cell values of two data tables
- Using LINQ. With two different lists. How can I identify objects that do not match
- How to get all records within the last Two Days from the current Date using EF 4?
More Query from same tag
- LINQ's ForEach on HashSet?
- IQueryable two tables from Code First Entity Framework
- How to truncate time part from date in linq query?
- Data is ignored at ThenInclude() for nested List property
- How to make this SQL query into DotNet Core 2.1 Linq
- Divide list in sublists by elements
- Ordering not working in Entity Framework query
- Filter data from MongoDB with .NET driver
- How to use function max for auto number?
- Linq to dataset select row based on max value of column
- Does calling Select() or GroupBy() in Linq to entities trigger querying the database?
- How to get 2 different values in 2 different tables using c# linq
- Group two lists by index using LINQ
- C# translate Func method signature into a new Func method signature
- multiple || after where
- LINQ - return all rows for a given ID if it contains xyz
- Update/Trim a value within a Lambda where expression
- LINQ - select from two arrays
- Accessing XElement's attribute with Linq
- Foreign Key Reference Already Has Value Exception Linq to SQL When Assigning Null
- How to apply not equal to with Linq XName
- 'System.Type' does not contain a definition for 'GenericTypeArguments' with (dynamic)
- Is there a way to parameterize the Contains portion of Linq when linked to an array
- Combining information from multiple lists
- Can I limit how many levels the .include adds with LINQ and Entity Framework 5?
- Correct MultiChoice Values in LINQ to SharePoint Query
- Left outer join in LINQ is showing exception
- Create flat list from tree ordered by the tree order
- Code review of this portion of Linq Filters
- Compare two lists, and get all differences?