score:19
You can use SequenceEqual
with additional order:
return list1.OrderBy(x => x).SequenceEqual(list2.OrderBy(x => x));
score:5
You can use All
and 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));
score:3
Try this:
bool equals = list1.OrderBy(x => x).SequenceEqual(list2.OrderBy(y => y));
I hope this helps ;)
score:0
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);
From MSDN:
The SetEquals method ignores duplicate entries and the order of elements in the other parameter.
Source: stackoverflow.com
Related Articles
- How to identify if two List<string> are equal regardless of the order?
- C# - code to order by a property using the property name as a string
- passing dynamic expression to order by in code first EF repository
- LINQ Source Code Available
- .NET 4 Code Contracts: "requires unproven: source != null"
- Removing a single item from an enumerable source when the items are equal
- creating Linq to sqlite dbml from DbLinq source code
- How to order a collection by a property first and then by another property regardless of tie on first property?
- Identify source of linq to sql query
- source code for LINQ 101 samples
- LINQ Order by another property is the first property is equal to a given value
- Will Linq query code in C# UWP project work slower or faster depending on order of its' parts?
- List or Array of String Contain specific word in Html Source Code
- c# Linq or code to extract groups from a single list of source data
- How to compare same property value regardless of order or duplication in multiple models
- Preserving order with LINQ
- Convert string[] to int[] in one line of code using LINQ
- Multiple Order By with LINQ
- Check if two lists are equal
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Value cannot be null. Parameter name: source
- LINQ order by null column where order is ascending and nulls should be last
- Linq to Objects: does GroupBy preserve order of elements?
- Linq order by boolean
- Does the order of LINQ functions matter?
- Linq code to select one item
- How do I find the text within a div in the source of a web page using C#
- Roslyn failed to compile code
- Linq Order by a specific number first then show all rest in order
- ascending/descending in LINQ - can one change the order via parameter?
- Query on DBcontext using value object returns client evaluation explicitly in EFCore 3.1
- Optimized code for Insert and Update in one method with LINQ?
- C# LINQ-SQL Help - Querying DB over 10,000 items
- How to filter child collection with linq dynamic
- Check if list1 contents matches list2 contents
- Optimizing a LINQ query with multiple joins and predicates
- How to process the XML file having hierarchical structure to get the inner details
- Is it possible to re-group the result of using a LINQ GroupBy()?
- Linq sub list Problem
- Sorting a List of Strings as Integers in LINQ C#
- When to use an extension method with lambda over LINQtoObjects to filter a collection?
- Linq query SUM and COUNT
- How to get a column value from data table with Linq
- Is there a way to reduce the number of hits on the db
- How to create a class in a dbml file dynamically
- Multiple condition on same column inside Linq where
- View Lambdas in Visual Studio Debugger
- Translating SQL Query to Linq for use with Entity Framework
- Find or Create Element in LINQ-to-XML
- LINQ Filtering a List of objects