Home > OS >  Is it possible to select items from IEnumerablle by two ids?
Is it possible to select items from IEnumerablle by two ids?

Time:02-28

I have this piece of code that I usually use to update/delete/create items

HashSet<int> current = new HashSet<int>(diagram.Links.Select(lnk => lnk.IDInput);
HashSet<int> received = new HashSet<int>(d.Links.Select(lnk => lnk.IDInput));
IEnumerable<int> delete = current.Except(received);
IEnumerable<int> new = received.Except(current);

And it works perfect when my list works with one ID but it this case I want to do something like diagram.Links.Select(lnk => lnk.IDInput AND lnk.IDOutput) Is it even possible or should I use Where instead?

Thank you in advance

CodePudding user response:

I would choose one of these 3 options

  1. Create a Dynamic object

     diagram.Links.Select(lnk => new {lnk.IDInput, lnk.IDOutput})
    
  2. Create a Touple object

     diagram.Links.Select(lnk => (lnk.IDInput, lnk.IDOutput))
    
  3. Create a Class / Struct and use it as the returned object

     public class IDHolder
     {
         public string IDInput;
         public string IDOutput;
     }
    
     diagram.Links.Select(lnk => new IDHolder() {IDInput = lnk.IDInput, IDOutput = lnk.IDOutput}))
    

All three would work, the latter is the most coherent in my taste, but choose whatever works best for you.

CodePudding user response:

For not change the other logic in your code you can simply use Concat like this:

HashSet<int> current = new HashSet<int>(diagram.Links.Select(lnk => lnk.IDInput)
.Concat(diagram.Links.Select(lnk => lnk.IDOutput)));

HashSet<int> received = new HashSet<int>(d.Links.Select(lnk => lnk.IDInput)
.Concat(d.Links.Select(lnk => lnk.IDOutput)));
  • Related