使用多列将linq连接到数据集查询

时间:2013-06-19 11:40:33

标签: c# linq .net-3.5

您好我正在尝试加入2个DataTables,其中一个包含3个不同的列作为主键,而另一个列与外键相同,这里是左数据表图像:

enter image description here

和Right DataTable Imge就是这个:

enter image description here

这里是我试图使用的代码,但没有返回,它应该

var query5 =
        from ChanToNGrsToNgrDAnWal in chanToNGrsToNgrDAnWal.AsEnumerable()
        join Node in node.AsEnumerable() 
            on new { 
                 A = ChanToNGrsToNgrDAnWal.Field<int?>("diffusers_Id"), 
                 B = ChanToNGrsToNgrDAnWal.Field<int?>("analogs_Id"), 
                 C = ChanToNGrsToNgrDAnWal.Field<int?>("wallstats_Id") }
            equals new { 
                 A = Node.Field<int?>("diffusers_Id"), 
                 B = Node.Field<int?>("analogs_Id"), 
                 C = Node.Field<int?>("wallstats_Id") 
            }                  
        select new
        {
            channel_Id = ChanToNGrsToNgrDAnWal.Field<int?>("channel_Id"),
            number = ChanToNGrsToNgrDAnWal.Field<string>("number"),
            nodeGroups_Id = ChanToNGrsToNgrDAnWal.Field<int>("nodeGroups_Id"),
            analogNodeSerial = ChanToNGrsToNgrDAnWal.Field<string>("analogNodeSerial"),
            diffuserNodeSerial = ChanToNGrsToNgrDAnWal.Field<string>("diffuserNodeSerial"),
            wallStatNodeSerial = ChanToNGrsToNgrDAnWal.Field<string>("wallStatNodeSerial"),
            diffusers_Id = ChanToNGrsToNgrDAnWal.Field<int?>("diffusers_Id"),
            analogs_Id = ChanToNGrsToNgrDAnWal.Field<int?>("analogs_Id"),
            wallstats_Id = ChanToNGrsToNgrDAnWal.Field<int?>("wallstats_Id"),
            typeCode = Node.Field<string>("typeCode"),
            node_Id = Node.Field<int?>("node_Id")

        };

任何人都可以告诉我为什么会这样,或者我写的查询是错误的,请你们帮助我。

1 个答案:

答案 0 :(得分:0)

根据您的示例数据,此处没有任何匹配项(零与空值不匹配)。如果由于您的应用程序逻辑而定,那么您可以将字段的值转换为int而不是Nullable<int>。在这种情况下,将为DbNull个单元格提供默认值(零):

    on new { 
         A = ChanToNGrsToNgrDAnWal.Field<int>("diffusers_Id"), 
         B = ChanToNGrsToNgrDAnWal.Field<int>("analogs_Id"), 
         C = ChanToNGrsToNgrDAnWal.Field<int>("wallstats_Id") 
    }
    equals new { 
         A = Node.Field<int>("diffusers_Id"), 
         B = Node.Field<int>("analogs_Id"), 
         C = Node.Field<int>("wallstats_Id") 
    }