4

我在使用 Linq Join 时遇到问题。我想加入 2 个表,它们具有与 n 列相同的结构。我的问题是我不知道这些列的名称,那么如何在 select new 中重写这些列?

表 1:这里我有一些 ID、Name 和 LastName 参数。注释、属性和其余为空

 ID    Name   LastName  Comment   Attribute ...     
                         "what"   "ABC"     ...
                         "hello"  "SDE"     ...
 3     lola               
 1              de           
 4     miki      
 ...   ...      ...      ...  

表 2:这里与表 1 相同,但在 Comment、Attribute 和其余部分中有一些参数。

 ID    Name   LastName  Comment   Attribute ...
                        "what"   "ABC"     ...
                        "hello"  "SDE"     ...
 1              de       "hi"     
 4     miki                      "OKK"
 3     lola             "yo"     "LL"

结果:我想像这样加入表格

 ID    Name   LastName  Comment   Attribute ...
                        "what"   "ABC"     ...
                        "hello"  "SDE"     ...
 3     lola               "yo"    "LL" 
 1               de        "hi"   
 4     miki                       "OKK"
 ...   ...      ...      ...       ...     ...

我的代码是:

var Result= from tb1 in table1.AsEnumerable()
            join tb2 in tabl2.AsEnumerable()
            on new
            {                
             Name = tb1.Field<String>("Name"),
             LastName = tb1.Field<String>("LastName"),
             } equals new
            {
             Name=tb2.Field<String>("Name"),
             LastName=tb2.Field<String>("LastName"),
             }
            into grp1
            from tb3 in grp1.DefaultIfEmpty()
            select new
    {
     ID = tb1.Field<String>("ID"),
     Name = tb1.Field<String>("Name") ,
     LastName = tb1.Field<String>("LastName"),
     Comment = tb3!= null ? tb3.Field<String>("Comment") : null,
     Attribute= tb3!= null ? tb3.Field<String>("Attribute") : null,
     ...
     // Here should be next Columns Name but don't know how to put there

     };

我尝试使用此代码,但我的编译器刚刚挂掉,不知道为什么

        for (int i = 2; i < table1.Rows.Count; i++)
        {
            foreach (DataRow dr in table2.Rows)
            {
                if ((table1.Rows[i]["Name"].ToString() == dr["Name"].ToString())&&table1.Rows[i]["LastName"].ToString() == dr["LastName"].ToString())
                {
                    table1.Rows.RemoveAt(i);
                    table1.ImportRow(dr);

                }
            }
        }
        dataGridView1.DataSource = table1;
4

3 回答 3

1

对于table1中的每一个row1,如果table2中有匹配的row2,则使用row2。否则,使用第 1 行。

var newTable = table1.Clone();
foreach (DataRow row1 in table1.Rows) // To skip the first 2, use table1.Rows.Cast<DataRow>().Skip(2)
{
    var row = table2.Rows.Cast<DataRow>().FirstOrDefault(row2 => 
                row1["Name"].ToString() == row2["Name"].ToString() &&
                row1["LastName"].ToString() == row2["LastName"].ToString()) ?? row1;
    newTable.ImportRow(row);
}
dataGridView1.DataSource = newTable;
于 2012-10-23T17:20:04.637 回答
1

像你一样加入然后将三个已知字段从 table1 的行复制到 table2 的行怎么样?

var copiedTable2 = table2.Copy(); // Copy table2 if you don't want it to be modified

var items = from tb1 in table1.AsEnumerable()
            join tb2 in copiedTable2.AsEnumerable()
            on new
            {                
                Name = tb1.Field<String>("Name"),
                LastName = tb1.Field<String>("LastName"),
            } equals new
            {
                Name=tb2.Field<String>("Name"),
                LastName=tb2.Field<String>("LastName"),
            }
            into grp1
            from tb3 in grp1.DefaultIfEmpty()
            select new
            {
                ID = tb1.Field<String>("ID"),
                Name = tb1.Field<String>("Name") ,
                LastName = tb1.Field<String>("LastName"),
                Row = tb3 ?? table2.NewRow();
            };

 foreach(var item in items)
 {
     item.Row.SetField<String>("ID", item.ID);
     item.Row.SetField<String>("Name", item.Name);
     item.Row.SetField<String>("LastName", item.LastName);
 }

 var rows = items.Select(x => x.Row);

 // How to set the rows as a DataGridView's DataSource
 var result = table2.Clone();
 foreach(var row in rows)
 {
     result.Rows.Add(row.ItemArray);
 }
 dataGridView.DataSource = result;
于 2012-10-23T14:17:57.887 回答
1

试试这个,不需要使用循环

var resultTable = from tb1 in table1.AsEnumerable()
                   join tb2 in tabl2.AsEnumerable()
                   on tb1["Name"].ToString() equals tb2["Name"].ToString()
                   where tb1["LastName"].ToString() == tb2["LastName"].ToString()
                   select r;

 DataTable resultTable =result.CopyToDataTable();
于 2014-06-02T11:52:23.263 回答