1

Using the following:

class A
{
    ...
    List<B> L;
}

class B
{
    string Id;
    string S;
}

I am trying to do an upsert of a B object in A.L but with B.Id as the key.

Right now, if I do AddToSet in in update, I will get an extra object in the L array if any field in B is different than what is already there.

AddToSet will consider:

B: { Id: "3", S: "1" }
B: { Id: "3", S: "2" }

as two different objects

So, I can add A.L.Any(x => x.Id == "1") to the search and then do a set in the update, but this will work to replace an object, and not insert it if nothing is found.

I am trying to find how to make the upsert in an array in one call since it needs to be atomic on my end.

How can I do that?


Clarification:

If I have the following data:

{
    L :
    [
        { Id: "1", S: "A" }
    ]
}

and I want to upsert: { Id: "2", S: "B" }

I would like the result to be:

{
    L :
    [
        { Id: "1", S: "A" },
        { Id: "2", S: "B" }
    ]
}

and then if I upsert: { Id: "2", S: "C" } I would like the output to be:

{
    L :
    [
        { Id: "1", S: "A" },
        { Id: "2", S: "C" }
    ]
}
Thomas
  • 10,933
  • 14
  • 65
  • 136

0 Answers0