c#datatable在此示例中左连接

时间:2014-11-09 14:33:28

标签: c# datatable

我有这段代码

var result = (from row1 in table.AsEnumerable()
                          join row2 in tabelPopup.AsEnumerable()
                          on row1.Field<string>("CallID") equals
                          row2.Field<string>("callID")
                          where row1.Field<string>("Direction") == "I"
                          select new
                          {
                              Agent = row1.Field<string>("Agent"),
                              StartTime = row1.Field<string>("StartTime"),
                              Reason = row2.Field<string>("Reason")
                          });

其中tabletablePopup是数据表,

如你所见,我正在加入。我想做一个左联盟,你可以帮助我plesae吗?

我经常阅读,但我发现我必须执行joininto,然后使用into name函数执行.DefaultOfEmpty(),但我不能

1 个答案:

答案 0 :(得分:0)

var left_outer_join = 
    from row1 in table.AsEnumerable() 
    join row2 in tabelPopup.AsEnumerable()
    on row1.Field<string>("CallID") equals row2.Field<string>("callID")
    into outJoin
    where row1.Field<string>("Direction") == "I"
    from oj in outJoin.DefaultIfEmpty() 
    select new {
        Agent = row1.Field<string>("Agent"),
        StartTime = row1.Field<string>("StartTime"),
        Reason = (oj == null) 
            ? String.Empty 
            : oj.Field<string>("Reason") 
    };