Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
bool Issuboption = option.SubProductOptions.ToList().All(s => getsuboptions.ToList().All(p => p.Id == s.Id));


option.SubProductOptions-This is one list

getsuboptions -This is another list

Both are having same type of objects and same items in both list but while programmatically while checking using Id's with above stament. I got False bool value. Actually we will need True

if anything wrong please correct it.

Please help me

Thanks

What I have tried:

Both are having same type of objects and same items in both list but while programmatically while checking using Id's with above stament. I got False bool value. Actually we will get True
Posted
Updated 18-Nov-22 10:31am
v3

To determine if 2 lists are equal, you would use either SequenceEqual or Union.

Let me show you why:
C#
List<Item> itemsA = new();
List<Item> itemsB = new();

for (int i = 0; i < 10; i++)
{
    itemsA.Add(new Item(i, $"item {i}"));
}

itemsB = itemsA.OrderBy(x => Guid.NewGuid()).ToList();

// SequenceEqual method
Console.WriteLine("SequenceEqual method");

var AreEqual = Enumerable.SequenceEqual(itemsA.OrderBy(e => e.Id), itemsB.OrderBy(e => e.Id));
Console.WriteLine($"Are Equal = {AreEqual}");

itemsB.Add(new(11, "imposter"));

AreEqual = Enumerable.SequenceEqual(itemsA.OrderBy(e => e.Id), itemsB.OrderBy(e => e.Id));
Console.WriteLine($"Are Equal = {AreEqual}");

Console.WriteLine();

// Intersect method
Console.WriteLine("Intersect method");

itemsB = itemsA.OrderBy(x => Guid.NewGuid()).ToList();

int count = itemsA.Select(item => item.Id).Intersect(itemsB.Select(item => item.Id)).Count();
Console.WriteLine($"Are Equal = {count == itemsA.Count}");

itemsB.Add(new(11, "imposter"));

count = itemsA.Select(item => item.Id).Intersect(itemsB.Select(item => item.Id)).Count();
Console.WriteLine($"Are Equal = {count == itemsA.Count}");

Console.WriteLine();

// Union method
Console.WriteLine("Union method");

itemsB = itemsA.OrderBy(x => Guid.NewGuid()).ToList();

count = itemsA.Select(item => item.Id).Union(itemsB.Select(item => item.Id)).Count();
Console.WriteLine($"Are Equal = {count == itemsA.Count}");

itemsB.Add(new(11, "imposter"));

count = itemsA.Select(item => item.Id).Union(itemsB.Select(item => item.Id)).Count();
Console.WriteLine($"Are Equal = {count == itemsA.Count}");

Console.WriteLine();

// Except method
Console.WriteLine("Except method");

itemsB = itemsA.OrderBy(x => Guid.NewGuid()).ToList();

count = itemsA.Select(item => item.Id).Except(itemsB.Select(item => item.Id)).Count();
Console.WriteLine($"Are Equal = {count == 0}");

itemsB.Add(new(11, "imposter"));

count = itemsA.Select(item => item.Id).Except(itemsB.Select(item => item.Id)).Count();
Console.WriteLine($"Are Equal = {count == 0}");

// mock class
record Item(int Id, string Name);

And the output:
SequenceEqual method
Are Equal = True
Are Equal = False

Intersect method
Are Equal = True
Are Equal = True

Union method
Are Equal = True
Are Equal = False

Except method
Are Equal = True
Are Equal = True

For Intersect and Except, you need an additional check:
C#
itemsA.Count == itemsB.Count && count == itemsA.Count


UPDATE

As requested, the SequenceEqual as an extension method:
C#
// extension method
Console.WriteLine("extension method");

itemsB = itemsA.OrderBy(x => Guid.NewGuid()).ToList();

Console.WriteLine($"Are Equal = {itemsA.Eqaulity(itemsB)}");

itemsB.Add(new(11, "imposter"));

Console.WriteLine($"Are Equal = {itemsA.Eqaulity(itemsB)}");

public static class ItemCompareExtension
{
    public static bool Eqaulity(this List<Item> itemsA, List<Item> itemsB)
        => Enumerable.SequenceEqual(itemsA.OrderBy(e => e.Id), itemsB.OrderBy(e => e.Id));
}
// mock class
public record Item(int Id, string Name);

Output:
extension method
Are Equal = True
Are Equal = False


UPDATE #2

Comparing 2 different lists:
C#
List<Item> itemsA = new();
List<Item> itemsB = new();

for (int i = 0; i < 10; i++)
{
    itemsA.Add(new Item(i, $"item a {i}"));
}

for (int i = 0; i < 10; i++)
{
    itemsB.Add(new Item(i, $"item b {i}"));
}

Console.WriteLine("extension method");

itemsB = itemsA.OrderBy(x => Guid.NewGuid()).ToList();

Console.WriteLine($"Are Equal = {itemsA.Eqaulity(itemsB)}");

itemsB.Add(new(11, "imposter"));

Console.WriteLine($"Are Equal = {itemsA.Eqaulity(itemsB)}");

Output:
extension method
Are Equal = True
Are Equal = False
 
Share this answer
 
v5
Comments
Krishna Veni 18-Nov-22 16:57pm    
Please give another approach if 2 two lists are equal.I was using list1.Equals.list2 that also returns false
Krishna Veni 18-Nov-22 16:58pm    
Sequence equal is good
Graeme_Grant 18-Nov-22 17:01pm    
Write it as an extension method.
Krishna Veni 18-Nov-22 17:04pm    
Which extension method
Graeme_Grant 18-Nov-22 17:06pm    
you have to do that yourself. I've updated the solution above.
All checks every element of the source - so if ListA is a superset of ListB then it will return false.

If you want to know if ListB is a subset of ListA, then I'd use Intersect instead: Enumerable.Intersect Method (System.Linq) | Microsoft Learn[^] with a custom IEquatable as shown in the examples.
If the intersection of the two collections has the same number of elements as the second collection, the first is a superset.
 
Share this answer
 
Comments
Graeme_Grant 18-Nov-22 16:51pm    
Both sets require an equal number of elements. if the second set has more, then the test will fail. You also need to compare the counts. Union is the better choice.
Krishna Veni 18-Nov-22 17:03pm    
Union is combine all elements
But in this just determine how if two lists have equals items
Krishna Veni 18-Nov-22 17:00pm    
Intersect common elements. I want if two lists have same number of items.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900