locked
How to compare two tables with LINQ RRS feed

  • Question

  • User2071107847 posted

    Hello

    my problem is about how to compare and update two tables with linq.

    my table structure is same with two tables

    id (key), name, x, y

    Sunday, March 29, 2020 12:24 PM

Answers

All replies

  • User303363814 posted

    There are a couple of ways to interpret your question.  Can you show the code that you have so far?  Give an example of what is wrong, tell us what you want to happen and the logic behind your requirement.

    Is one table the master and you want the other table to have the same "name, x, y" when the id values are the same?  What if the id does not exist in the second table?  What if the id is in the second table but nor the first table?

    Are both tables considered equal?  Does that mean that if one id is in TableA but not TableB then you insert it and vice versa.  Or, should the comparison be made based on 'name'  What if the same id/name is in both tables but the value of 'y' is different, what do you want to do?

    Work out what you want to do in every possible situation and start writing code to implement that algorithm.  If you get stuck then show us what you have so far and tell us why you are stuck.

    Sunday, March 29, 2020 10:40 PM
  • User1535942433 posted

    Hi aprangulashvili,

    Accroding to your description,I don't understand your requirment clearly.I'm guessing you want to compare two tables' value with linq.

    Could you tell us more details about your requirment and post the result you want to get.

    If you want to compare two tables' values, I suggest you could use except to find the differents.

    More details,you could refer to below article:

    https://forums.asp.net/t/2048949.aspx?Compare+two+data+tables+for+differences+using+LINQ

    Best regards,

    Yijing Sun

    Monday, March 30, 2020 6:36 AM
  • User2071107847 posted

    There are a couple of ways to interpret your question.  Can you show the code that you have so far?  Give an example of what is wrong, tell us what you want to happen and the logic behind your requirement.

    Is one table the master and you want the other table to have the same "name, x, y" when the id values are the same?  What if the id does not exist in the second table?  What if the id is in the second table but nor the first table?

    Are both tables considered equal?  Does that mean that if one id is in TableA but not TableB then you insert it and vice versa.  Or, should the comparison be made based on 'name'  What if the same id/name is in both tables but the value of 'y' is different, what do you want to do?

    Work out what you want to do in every possible situation and start writing code to implement that algorithm.  If you get stuck then show us what you have so far and tell us why you are stuck.

    Hello Paul

    Thank you for reply. My problem is simple. i have 2 tables. first is master and second is like temporary. e.g when i press button parsing data from XML then i'm putting it in temp table and here is my problem. the files i'm parsing doesn't deleting it is growing every day and i want to take differential data from temporary tables. i know there are ways to compare data in process parsing but this way i think slow down process, because first process is read data from XML and there i'm already waiting process :). Therefore i created second table and first i'm putting data there. now i want to take this differential data from temp table and insert into master table. please look simple if you want to help me :)

    Thank you again

    Friday, April 3, 2020 12:46 PM
  • User-943250815 posted

    Sounds you looking for Linq "Except" (Produces the set difference of two sequences).
    https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.except?view=netframework-4.8

    There is also Linq "Intersect" (Produces the set intersection of two sequences.),
    https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.intersect?view=netframework-4.8

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 3, 2020 2:09 PM