0

I know this question has been reiterated multiple times, and I think this is straightforward when using lists of reference types. That is also what most questions talk about. When doing this with value types, I can't quite figure it out.

What I want do to is take an incoming request, and ensure that the request has at least the same numbers that are already present. Additional numbers are allowed, and the additional numbers in the request will be persisted. So if [1,2] is already persisted and the request contains [1,2,3], 3 will be persisted. If the request contained only 1 or 2, that would be invalid, and [1,2] would be valid and only the data that already existed would be returned.

The last case mentioned above I've implemented the following way:

Enumerable.SequenceEqual(existing.OrderBy(e => e), request.OrderBy(e => e));

When the lists aren't equal though, I want to treat each int in a list as if it was unique, but I can't figure out how to do this. In the following example, I would like the resulting list to be [1, 4], but it ends up being [4] due to both the numbers 1 in the existing list are excluded due to being equal.

var existing = new List<int> { 1, 1, 2, 3, 4 };
var request = new List<int> { 1, 2, 3 };
existing.Except(request).ToList();
// 4

I have seen that Except accepts a IEqualityComparer, but I stumble when trying to implement this for a value type and I don't know if this is the correct way to go.

Any help on how I should think when approaching this problem would be greatly appreciated. Efficiency is not that important, as the lists will not contain many elements at all.

jokarl
  • 1,913
  • 2
  • 24
  • 52
  • As far as I understood we have two sources (incoming numbers, already persisted numbers) and one outcome (outgoing numbers). Could you write down a table or something similar describing the input lists and the expected outcome (e.g. `([1,2], [1,2,4]) -> [1,2,4]`? Maybe also as output `error` or something like this, if needed. – Oliver Sep 15 '22 at 11:47

1 Answers1

2

You could use regular List<T>.Remove(T):

foreach(var e in existing)
{
    var actuallyExisted = request.Remove(e);
}

actuallyExisted will be false if it couldn't find e to remove. request will now contain all ints that weren't in existing.

Yitz
  • 946
  • 4
  • 8
  • I'm a bit mad at myself for staring blindly at utility functions when this solves my problem – jokarl Sep 15 '22 at 11:47
  • Yeah not having to care about efficiency allows you to think of the old fashioned simple way of doing things... – Yitz Sep 15 '22 at 11:51
  • I ended up going with something similar do this due to the simplicity and my overall skill level as well as my teams. Thank you! – jokarl Sep 15 '22 at 11:59