join 2 datarow rows into one row C#

后端 未结 3 1656
不知归路
不知归路 2021-01-17 02:32

while i know i can make a join of 2 rows via sql, my program doesn\'t use it i have 2 datatables and i take each row, compare to the rows on the other table and want to make

3条回答
  •  日久生厌
    2021-01-17 03:00

    Here's an example of two ways to do join using LINQ.

            var t1 = new DataTable();
            var t2 = new DataTable();
            t1.Columns.Add("id", typeof (Int32));
            t1.Columns.Add("data", typeof (String));
            t2.Columns.Add("id", typeof (Int32));
            t2.Columns.Add("data", typeof (Int32));
    
            t1.Rows.Add(new {id=1, data="John"});
            t1.Rows.Add(new {id = 2, data = "Mary"});
    
            t2.Rows.Add(new {id = 1, data = "100"});
            t2.Rows.Add(new {id = 2, data = "200"});
    
    
            var results = from x in t1.Select()
                          join y in t2.Select() on (Int32) x["id"] equals (Int32) y["id"]
                          select (new {id = x["id"], name = x["data"], number = y["data"]});
    
            var lamdaResults = t1.Select().Join(
                t2.Select(), x => x["id"], y => y["id"],
                (x, y) => new {id=x["id"], name=x["data"], number=y["data"]});
    

提交回复
热议问题