Linq加入了不同的

时间:2016-09-16 04:48:07

标签: c# mysql asp.net-mvc linq lambda

我有两张桌子 TABLE_A

id col_A  col_B  Col_C  DataAmt  value
1   a       b       c     1       1
2   a       b       c     1       1
3   a       b       c     2       2
4   a       b       c     2       2 
5   a       b       c     2       2 

表-B

id  col_A   col_B   col_C
1    a       b        c
2    a       b        c
3    a       b        c

我的linq查询

from table_B in db.Table_B
join table_A in db.table_A
on new
{
  col_A = table_B.col_A,
  col_B = table_B.col_B,
  col_C = table_B.col_C,
}
equals new
{
  col_A = table_A.col_A,
  col_B = table_A.col_B,
  col_C = table_A.col_C,
}
select table_A

在我的for i两个字段中,一个用于 Value_A Value_B
从表B到表A查询三个colms。我必须明确 col_A col_B col_C DataAmt 值<的值/ strong> col in TABLE_A 大于1应该放在 Value_A 字段中,其他字符放在 Value_B 中。
我不能采取不同的价值观和价值观的分离。

2 个答案:

答案 0 :(得分:0)

也许是这样的,根据需要改变select子句:

(from table_B in db.Table_B
join table_A in db.table_A
on new
{
  col_A = table_B.col_A,
  col_B = table_B.col_B,
  col_C = table_B.col_C,
}
equals new
{
  col_A = table_A.col_A,
  col_B = table_A.col_B,
  col_C = table_A.col_C,
}
select new 
{
   col_A = table_A.value > 1 ? table_A.value : table_A.col_A,
   col_B = table_A.value > 1 ? table_A.Col_B : table_A.value: ,
   col_C = table_A.col_C,
   DataAmt = table_A.DataAmt
}).Distinct();

答案 1 :(得分:0)

static void Main(string[] args)
    {
        List<TableA> tableA = new List<TableA>
        {
            new TableA { Id = 1, A = "a", B = "b", C = "c", DataAmt = 1, Value = 1 },
            new TableA { Id = 2, A = "a", B = "b", C = "c", DataAmt = 1, Value = 1 },
            new TableA { Id = 3, A = "a", B = "b", C = "c", DataAmt = 2, Value = 2 },
            new TableA { Id = 4, A = "a", B = "b", C = "c", DataAmt = 2, Value = 2 },
            new TableA { Id = 5, A = "a", B = "b", C = "c", DataAmt = 2, Value = 2 },
        };

        List<TableB> tableB = new List<TableB>
        {
            new TableB { Id = 1, A = "a", B = "b", C = "c" },
            new TableB { Id = 2, A = "a", B = "b", C = "c" },
            new TableB { Id = 3, A = "a", B = "b", C = "c" },
        };

        var result = tableA.Where(x => x.Value > 1 && tableB.Any(y => y.A == x.A && y.B == x.B && y.C == x.C)).Select(x => new
        {
            A = x.A,
            B = x.B,
            C = x.B,
            DataAmt = x.DataAmt
        }).Distinct().ToList();
    }