LINQ Comparing two List<T>

Sudip Bhatt 2,276 Reputation points
2021-01-20T07:51:51.65+00:00

From this post https://codereview.stackexchange.com/questions/7422/compare-items-in-two-lists
i found many people give many solution to compare two list<T>. i have few question about their code.

Which approach perform best when two list will have large data ?

1st approach

internal bool DoIdsMatchThoseFromXml(List<string> ids, List<string> xmlIds)
{
    return !ids.Except(xmlIds).Any(); // A - B = {}
}

i understand this approach how it is working and comparing two list.

2nd approach

internal bool DoIdsMatchThoseFromXml(List<string> ids, List<string> xmlIds)
{
    return ids.Count == xmlIds.Count // assumes unique values in each list
        && new HashSet<string>(ids).SetEquals(xmlIds);
}

just do not understand this line new HashSet<string>(ids).SetEquals(xmlIds);
what kind of action SetEquals() is doing here?

3rd Approach

internal bool DoIdsMatchThoseFromXml(List<string> Ids, List<string> XmlIds)
{
    return 
        Ids.Count == XmlIds.Count &&
        Ids.All(XmlIds.Contains) &&
        XmlIds.All(Ids.Contains);
}

this checking Ids.All(XmlIds.Contains) would not be sufficient ? why they are checking
Ids.All(XmlIds.Contains) and XmlIds.All(Ids.Contains); both any idea ?

4th Approach

internal static bool DoIdsMatchThoseFromXml(List<string> Ids, List<string> XmlIds)
    {
        return Ids.TrueForAll(XmlIds.Contains) && XmlIds.TrueForAll(Ids.Contains);
    }

what TrueForAll() function does ?

5th Approach

internal bool DoIdsMatchThoseFromXml(List<string> Ids, List<string> XmlIds)
    {
        Ids.Sort();
        XmlIds.Sort();

        return Ids.SequenceEqual(XmlIds);
    }

what SequenceEqual() does here ?

6th Approach

internal bool DoIdsMatchThoseFromXml(List<string> Ids, List<string> XmlIds) {
  if (Ids.Count != XmlIds.Count) return false;
  HashSet<string> xmlIds = new HashSet<string>(XmlIds);
  return Ids.All(id => xmlIds.Contains(id));
}

please tell me how below two line is working
HashSet<string> xmlIds = new HashSet<string>(XmlIds);
return Ids.All(id => xmlIds.Contains(id));

Which approach perform best when two list will have large data ?

please explain which help me to understand how various approaches is working. Thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Tom-9560 15 Reputation points
    2023-03-05T15:22:55.5666667+00:00

    Why does M$ have this "Learn" platform, on which one usually never learns anything, except if someone posts a link to e.g. StackOverflow?

    For many years, it's evident that M$ is just interested in getting the most money possible while hopefully not having to offer any support.

    Close this kind of M$ Website like "learn", "feedback", "uservoice", etc. would help paying customers not lose time on mostly useless M$ "Support" sites and M$ does not have to pay employees who try to maintain those pages at zero costs.

    Thanks a lot, kind regards,
    Thomas

    0 comments No comments