0

我正在使用下面的内部连接来检索两个表之间的数据,但所有数据都没有被填充。我尝试通过使用 CCY1== CCY1 和 PCODE == PCODE 连接来实现外连接,但没有运气。

在此处输入图像描述

    var q = from g1 in TableCCY1.AsEnumerable()
            join g2 in TableCCY2.AsEnumerable()
            on g1.Field<string>("CCY1") equals g2.Field<string>("CCY1") 
            where  g1.Field<string>("PCODE") == g2.Field<string>("PCODE")
                   select new
                     {
                             g1currency = g1.Field<string>("CCY1"),
                             g2currency = g2.Field<string>("CCY1"),
                             g1code = g1.Field<string>("PCODE"),
                             g2code = g2.Field<string>("PCODE"),
                             g1Amt1 = g1.Field<string>("AMT1"),
                             g2Amt2 = g2.Field<string>("AMT2")
                     };

谢谢你的帮助。

4

2 回答 2

2

对于左连接,您可以使用此方法:http: //msdn.microsoft.com/en-us/library/vstudio/bb397895.aspx

代码应该是:

var q = from g1 in TableCCY1
            join g2 in TableCCY2 on g1.CCY1 equals g2.CCY1 && g1.PCODE equals g2.PCODE into TableCCY3
from g3 in TableCCY3.DefaultIfEmpty()
 select new
                     {
                             g1currency = g1.CCY1,
                             g2currency = (g3 == null ? String.Empty : g3.CCY1),
                             g1code = g1.PCODE,
                             g2code = (g3 == null ? String.Empty : g3.PCODE),
                             g1Amt1 = g1.AMT1,
                             g2Amt2 = (g3 == null ? 0 : g3.AMT2)
                     };
于 2013-10-31T20:57:48.863 回答
1

看起来您只想将两个表合并/合并为一个,然后只对这两列进行分组。您没有在逻辑上连接这两个表。这实际上使它变得容易得多。

var q = from row in TableCCY1.AsEnumerable().Concat(TableCCY2.AsEnumerable())
        group row by new
        {
            CCY1 = row.Field<string>("CCY1"),
            PCode = row.Field<string>("PCODE")
        } into matches
        select new
        {
            CCY1 = matches.Key.CCY1,
            PCODE = matches.Key.PCode,
            Sum = matches.Sum(match => match.Field<decimal?>("AMT2")),
        };
于 2013-10-31T20:58:46.817 回答