locked
how to get the following resutl RRS feed

  • Question

  • I have got two List<int> variable listA and listB. In listA I have {1,2,3,4}, in listB I have {1,2,3,5} , so I will update database record  based on listA and listB. The data from listA are from database, and I will update database with delecting Id=4 and insert Id=5. so How to do this.

    :) thank you very much.

    Monday, June 5, 2017 12:49 PM

Answers

  • To identify the deletes, get all IDs from listA that aren't in listB. Assuming you are truly storing just ints then Except works well. To get the values to insert you can go the other way.

    var deletes = listA.Except(listB);
    var inserts = listB.Except(listA);

    For more complex objects you'll have to use a comparer to properly match the values.

    Michael Taylor
    http://www.michaeltaylorp3.net

    • Marked as answer by Oscar_Wu Monday, June 5, 2017 2:14 PM
    Monday, June 5, 2017 1:54 PM

All replies

  • Hi qing_,

    You could just get the index of Id=4 from list A and use that index value to get the Id in listB.


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Monday, June 5, 2017 1:19 PM
  • To identify the deletes, get all IDs from listA that aren't in listB. Assuming you are truly storing just ints then Except works well. To get the values to insert you can go the other way.

    var deletes = listA.Except(listB);
    var inserts = listB.Except(listA);

    For more complex objects you'll have to use a comparer to properly match the values.

    Michael Taylor
    http://www.michaeltaylorp3.net

    • Marked as answer by Oscar_Wu Monday, June 5, 2017 2:14 PM
    Monday, June 5, 2017 1:54 PM
  • Thank you very much.
    Monday, June 5, 2017 2:14 PM