DataTable中的LINQ加入许多列 [英] DataTable Linq join many columns

查看:190
本文介绍了DataTable中的LINQ加入许多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Linq的麻烦加入。我想加入2表,这是他们与n列相同的结构。我的问题是我不知道这些列的名字,所以,我怎么可以重写那些在选择新的



表1:在这里,我有身份证一些参数,名字和姓氏。注释,属性,其余都为空

  ID名称姓氏评论...属性
是什么ABC ...
你好,SDE......
3萝拉
1日
4美姬
... ... ... ...

表2:这里是同样喜欢表1,但它在注释,属性和一些参数休息。

  ID名称姓氏评论...属性
是什么ABC...
你好SDE......
1日的hi
4三木OKK
3萝拉哟,LL

结果:我想加入表这样的

  ID名称姓氏评论...属性
是什么ABC...
你好,SDE......
3萝拉哟,LL
1日的hi
4三木OKK
... ... ... ... ... ...

我的代码是:

  VAR结果=从TB1在table1.AsEnumerable()
在tabl2.AsEnumerable加入TB2()新的
{
名称= tb1.Field<
;弦乐>(姓名),
姓氏= tb1.Field<弦乐>(姓氏),
}等于新的
{
名称= tb2.Field<弦乐>(姓名),
姓氏= tb2.Field<弦乐>(姓氏),
}
到来自TB3在grp1.DefaultIfEmpty()
GRP1
选择新的
{
ID = tb1.Field<弦乐>(ID),
名称= tb1.Field<弦乐>(姓名),
姓氏= tb1.Field<弦乐>(姓 ),
注释= TB3!= NULL? tb3.Field<串GT;(注释):空,
属性= TB3!= NULL? tb3.Field<串GT(属性):空,
...
//这里应该是下一个字段名字,但不知道如何把有

};






我试着用这个代码,但我的编译器只是挂出,不知道为什么。

 的for(int i = 2; I< table1.Rows.Count;我++)
{
的foreach(在table2.Rows的DataRow DR)
{
如果((table1.Rows [I] [名称]。的ToString()==博士[名称]的ToString())及和放大器; table1.Rows [I] [姓氏]的ToString()==博士[姓氏]的ToString())
{
table1.Rows。 RemoveAt移除(ⅰ);
table1.ImportRow(DR);

}
}
}
dataGridView1.DataSource = table1的;


解决方案

对于表1各ROW1,如果有一个匹配ROW2表2中,使用ROW2。否则,使用ROW1

  VAR newtable的= table1.Clone(); 
的foreach(在table1.Rows的DataRow ROW1)//跳过第2,使用table1.Rows.Cast< DataRow的方式>()跳过(2)
{
无功行=表2 .Rows.Cast< DataRow的>()FirstOrDefault(ROW2 =方式>
ROW1 [名称]的ToString()== ROW2 [名称]的ToString()及。和放大器;
ROW1 [姓氏]。的ToString()== ROW2 [姓氏]。的ToString())? ROW1;
newTable.ImportRow(行);
}
dataGridView1.DataSource = newtable的;


I have a trouble with Linq Join. I want to join 2 tables, which they have same structure with n-columns. My problem is i don't know the names of those columns, so how can i rewrite those in select new?

Table 1: Here I have some parameters in ID, Name and LastName. Comment, Attribute and the rest are null

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

Table 2: Here is the same like Table 1 but it has some parameters in Comment, Attribute and the Rest.

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

Result: I would like to have joined Table like this

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

My Code would be:

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

     };


I tried with this code but my compiler just hanged out, dont know why

        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;

解决方案

For each row1 in table1, if there is a matching row2 in table2, use row2. Otherwise, use row1.

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;

这篇关于DataTable中的LINQ加入许多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆