How to subtract one huge list from another efficiently in C#

后端 未结 4 881
天命终不由人
天命终不由人 2021-02-07 00:09

I have a very long list of Ids (integers) that represents all the items that are currently in my database:

var idList = GetAllIds();

I also hav

4条回答
  •  长发绾君心
    2021-02-07 00:44

    Assuming the following premises are true:

    • idList and itemsToAdd may not contain duplicate values
    • you are using the .NET Framework 4.0

    you could use a HashSet this way:

    var itemsToAddSet = new HashSet(itemsToAdd);
    itemsToAddSet.ExceptWith(idList);
    

    According to the documentation the ISet.ExceptWith method is pretty efficient:

    This method is an O(n) operation, where n is the number of elements in the other parameter.

    In your case n is the number of items in idList.

提交回复
热议问题