C#LINQ join DATABLES如果不为空,则显示数据

C#LINQ join DATABLES如果不为空,则显示数据,c#,linq,datatable,C#,Linq,Datatable,我有两个数据表。我已经通过LINQ查询加入了数据表 var rows = wdt.AsEnumerable().Join( mdt.AsEnumerable(), wdtRows => wdtRows.Field<int?>("userID1"), (wdtRows, mdtRows1) => new { wdtRows, mdtRows1 }

我有两个数据表。我已经通过LINQ查询加入了数据表

var rows = wdt.AsEnumerable().Join(
                    mdt.AsEnumerable(),
                    wdtRows => wdtRows.Field<int?>("userID1"),
                    (wdtRows, mdtRows1) => new { wdtRows, mdtRows1 }
                    ) //New anon<datarow,datarow> - I will call anons 'n' (for new) below
                    .Join(
                    mdt.AsEnumerable(),
                    n => n.wdtRows.Field<int?>("userID2"),
                    mdtRows2 => mdtRows2.Field<int>("iD"),
                    (n, mdtRows2) => new { n.wdtRows, n.mdtRows1, mdtRows2 }
                    )//New anon<datarow,datarow,datarow>
                    .Join(
                    mdt.AsEnumerable(),
                    n => n.wdtRows.Field<int?>("userID3"),
                    mdtRows3 => mdtRows3.Field<int>("iD"),
                    (n, mdtRows3) => new { n.wdtRows, n.mdtRows1, n.mdtRows2, mdtRows3 }
                    ) //New anon<datarow,datarow,datarow,datarow> - all joined up now
                    .ToList();

                rows.ForEach(r =>
                {
                    var datarow = dt.NewRow();
                    datarow[0] = r.mdtRows1.Field<string>("firstName") + " " + r.mdtRows1.Field<string>("lastName");
                    datarow[1] = r.mdtRows2.Field<string>("firstName") + " " + r.mdtRows2.Field<string>("lastName");
                    datarow[2] = r.mdtRows3.Field<string>("firstName") + " " + r.mdtRows3.Field<string>("lastName");
                    datarow[3] = r.wdtRows[3].ToString();

                    //If you can find a way to add a range (or turn a List<datarow> into a table)
                    //this you can change this section to a select and return the datarow instead
                    dt.Rows.Add(datarow);

数据表2:

Code        userid1    userid2      userid3  work
    1f           1           3           6       gg
    2g           1           4           7       gg
    3b           3           4           7       gg
    4v           4           3           8       gg
新数据表:

Code    username1   username2   username3  work
1f           a           b           c       gg
2g           d           f           r       gg
3b           c           h           g       gg
4v           d           s           h       gg

很难理解你想要达到的目标。。。应该有强类型的datarows(假设您使用设计器定义了数据表)可以简化此任务。在连接方面,方法语法确实很难理解。出于这个原因,我总是将一个查询拆分为多个部分,连接部分作为查询,其余部分使用方法语法。因为连接是延迟执行的,所以它同样有效。作为一种替代,不要将
int
-字段转换为
string
,并将其解析为
int
,而是使用
row.field(“ColumnName”)
<代码>字段还支持可为空的类型。所以它很简单:
int?userID2=n.wdtRows.Field(“userID2”)
。这比:
Convert.ToInt32(string.IsNullOrEmpty(n.wdtRows[“userID2”].ToString())?0:n.wdtRows[“userID2”])
更具可读性和效率。在加入Datatable1之前,需要将Datatable2展平为:code,ID,work。
Code    username1   username2   username3  work
1f           a           b           c       gg
2g           d           f           r       gg
3b           c           h           g       gg
4v           d           s           h       gg