Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. LinQ: sub items and root in DataTable

LinQ: sub items and root in DataTable

Scheduled Pinned Locked Moved LINQ
csharpquestionlinqmcphelp
4 Posts 3 Posters 13 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cuong nm
    wrote on last edited by
    #1

    Hi ! I have some codes below here: DataTable objTable = new DataTable(); objTable.Columns.Add("ID"); objTable.Columns.Add("Name"); objTable.Columns.Add("Root"); DataRow objrow1 = objTable.NewRow(); objrow1["ID"] = "1"; objrow1["Name"] = "1"; objrow1["Root"] = ""; objTable.Rows.Add(objrow1); objrow1 = objTable.NewRow(); objrow1["ID"] = "11"; objrow1["Name"] = "11"; objrow1["Root"] = "1"; objTable.Rows.Add(objrow1); objrow1 = objTable.NewRow(); objrow1["ID"] = "12"; objrow1["Name"] = "12"; objrow1["Root"] = "1"; objTable.Rows.Add(objrow1); objrow1 = objTable.NewRow(); objrow1["ID"] = "121"; objrow1["Name"] = "121"; objrow1["Root"] = "12"; objTable.Rows.Add(objrow1); objrow1 = objTable.NewRow(); objrow1["ID"] = "2"; objrow1["Name"] = "2"; objrow1["Root"] = ""; objTable.Rows.Add(objrow1); objrow1 = objTable.NewRow(); objrow1["ID"] = "21"; objrow1["Name"] = "21"; objrow1["Root"] = "2"; objTable.Rows.Add(objrow1); And now, how can I list item and subitems in root is 1?Please help me !!!!

    MCP.NET, MCAD.NET, MCSD.NET

    S X 2 Replies Last reply
    0
    • C cuong nm

      Hi ! I have some codes below here: DataTable objTable = new DataTable(); objTable.Columns.Add("ID"); objTable.Columns.Add("Name"); objTable.Columns.Add("Root"); DataRow objrow1 = objTable.NewRow(); objrow1["ID"] = "1"; objrow1["Name"] = "1"; objrow1["Root"] = ""; objTable.Rows.Add(objrow1); objrow1 = objTable.NewRow(); objrow1["ID"] = "11"; objrow1["Name"] = "11"; objrow1["Root"] = "1"; objTable.Rows.Add(objrow1); objrow1 = objTable.NewRow(); objrow1["ID"] = "12"; objrow1["Name"] = "12"; objrow1["Root"] = "1"; objTable.Rows.Add(objrow1); objrow1 = objTable.NewRow(); objrow1["ID"] = "121"; objrow1["Name"] = "121"; objrow1["Root"] = "12"; objTable.Rows.Add(objrow1); objrow1 = objTable.NewRow(); objrow1["ID"] = "2"; objrow1["Name"] = "2"; objrow1["Root"] = ""; objTable.Rows.Add(objrow1); objrow1 = objTable.NewRow(); objrow1["ID"] = "21"; objrow1["Name"] = "21"; objrow1["Root"] = "2"; objTable.Rows.Add(objrow1); And now, how can I list item and subitems in root is 1?Please help me !!!!

      MCP.NET, MCAD.NET, MCSD.NET

      S Offline
      S Offline
      Simon_Whale
      wrote on last edited by
      #2

      you will need to make sure that there is a reference to System.Data.DataSetExtensions in your project.

      var Results = from data in objTable.AsEnumerable()
      where data.Field("Root") == 1
      select data

      further reading Linq query on a datatable[^] Queries in LINQ to DataSet[^]

      Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

      C 1 Reply Last reply
      0
      • S Simon_Whale

        you will need to make sure that there is a reference to System.Data.DataSetExtensions in your project.

        var Results = from data in objTable.AsEnumerable()
        where data.Field("Root") == 1
        select data

        further reading Linq query on a datatable[^] Queries in LINQ to DataSet[^]

        Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

        C Offline
        C Offline
        cuong nm
        wrote on last edited by
        #3

        Thanks ! It return two items: 11 and 12. But sub items of 12? How can I get its?

        MCP.NET, MCAD.NET, MCSD.NET

        1 Reply Last reply
        0
        • C cuong nm

          Hi ! I have some codes below here: DataTable objTable = new DataTable(); objTable.Columns.Add("ID"); objTable.Columns.Add("Name"); objTable.Columns.Add("Root"); DataRow objrow1 = objTable.NewRow(); objrow1["ID"] = "1"; objrow1["Name"] = "1"; objrow1["Root"] = ""; objTable.Rows.Add(objrow1); objrow1 = objTable.NewRow(); objrow1["ID"] = "11"; objrow1["Name"] = "11"; objrow1["Root"] = "1"; objTable.Rows.Add(objrow1); objrow1 = objTable.NewRow(); objrow1["ID"] = "12"; objrow1["Name"] = "12"; objrow1["Root"] = "1"; objTable.Rows.Add(objrow1); objrow1 = objTable.NewRow(); objrow1["ID"] = "121"; objrow1["Name"] = "121"; objrow1["Root"] = "12"; objTable.Rows.Add(objrow1); objrow1 = objTable.NewRow(); objrow1["ID"] = "2"; objrow1["Name"] = "2"; objrow1["Root"] = ""; objTable.Rows.Add(objrow1); objrow1 = objTable.NewRow(); objrow1["ID"] = "21"; objrow1["Name"] = "21"; objrow1["Root"] = "2"; objTable.Rows.Add(objrow1); And now, how can I list item and subitems in root is 1?Please help me !!!!

          MCP.NET, MCAD.NET, MCSD.NET

          X Offline
          X Offline
          Xiecm_China
          wrote on last edited by
          #4

          var list = objTable.AsEnumerable();

                  var query = from l in list
                      where l.Field<string>("root") == "1"
                      select l;
          
                  var result = query.Concat(from q in query
                      from l in list
                      where l.Field<string>("root") == q.Field<string>("id")
                      select l);
          
                  foreach (var r in result)
                  {
                      Console.WriteLine("value has id:{0}, root:{1}", r.Field<string>("id"), r.Field<string>("root"));
                  }
          
          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups