none
Writing a LINQ to DataTable that updates values RRS feed

  • Question

  • Note that these are the not real table and column names; I am simplifying this use case.

    • I have two data tables, named MyTests and Tests.
    • Tests uses a single primary key TestID and contains several columns that store useful data, like Selected (boolean).
    • MyTests uses two foreign-key columns as its primary key {MyID and TestID} and also contains several columns that store useful data.

    On a user event, the application must to *populate* all Selected values of the Tests data table using this logic: set Selected to True when a current MyTests data row exists having a matching TestID; set Selected to False otherwise.

    Note: by *populate*, I mean the application will be changing the local values in the current Tests data rows; I do not mean that the application will be updating the data server.

    I am new to LINQ.  Please help.  What is the code to implement that LINQ to datatable query?

     

    The original question incorrectly stated that the Selected column was in MyTests; it should have said that it is in Tests.  Sorry.

    Tuesday, April 6, 2010 8:30 PM

Answers

  • Hi Paul,

    You can use LINQ to Select the matching ID rows and from there you can assign the Selected Column, here's a sample

    IEnumerable<DataRow> joinID = from MyTest in myTest.AsEnumerable()
                                  join Test in test.AsEnumerable() on MyTest.Field<int>("TestID") equals Test.Field<int>("TestID")
                                  select MyTest;
    foreach (DataRow row in joinID)
    {
        row["Selected"] = true;
    }

    Please let me know if this helps

    Juan


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Saturday, April 10, 2010 12:47 AM
    Moderator

All replies

  • Hello,

    First set all Selected column to false for every row in the MyTests DataTable. This is just like a reset job.

    After that, we can iterate through rows in Tests.MyTests, set all Selected column to true.

    foreach(DataRow row in MyTests.Rows)

    {

         row.Selected = false;

    }

    foreach(DataRow rowTests in Tests.Row)

    {

         foreach(DataRow rowMyTests in rowTests.MyTests.Rows)

         {

              rowMyTests.Selected = true;

         }

    }

     

    Best regards,

    Ji Zhou


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, April 7, 2010 11:11 AM
    Moderator
  • Thanks for your quick reply.  I realize that I stated something incorrectly in my original phrasing of the question, which I've fixed.  And so I sort of agree with your answer (based on my bad info).  Being a VB.NET developer, this is how I could code this situation without LINQ:

    For Each drTest As DataRow In mdtInspectionTests.Rows
        drTest(InspectionTest.Select) = False
    Next
    For Each drTemplateTest As DataRow In mdtServiceTemplateInspectionTests.Rows
        For Each drTest As DataRow In mdtInspectionTests.Rows
            If drTemplateTest(ServiceTemplateInspectionTest.InspectionTestID) = _
               drTest(InspectionTest.InspectionTestID) Then
                 drTest(InspectionTest.Select) = True
            End If
        Next
    Next
    

    But the reason I asked the question was to learn about how to code using LINQ.  How would I accomplish this with LINQ to DataTable?

    Wednesday, April 7, 2010 12:34 PM
  • Anyone?  Or perhaps this cannot be done using LINQ?
    Friday, April 9, 2010 3:02 PM
  • Hi Paul,

    You can use LINQ to Select the matching ID rows and from there you can assign the Selected Column, here's a sample

    IEnumerable<DataRow> joinID = from MyTest in myTest.AsEnumerable()
                                  join Test in test.AsEnumerable() on MyTest.Field<int>("TestID") equals Test.Field<int>("TestID")
                                  select MyTest;
    foreach (DataRow row in joinID)
    {
        row["Selected"] = true;
    }

    Please let me know if this helps

    Juan


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Saturday, April 10, 2010 12:47 AM
    Moderator
  • It helped a lot. this was the solution i was looking for Thanks a lot.

    May Allah bless this world with peace. (Amen)

    Friday, March 22, 2013 11:04 AM