[英]Linq Join with Distinct
I have two table TABLE_A 我有两个表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
TABLE_B TABLE_B
id col_A col_B col_C
1 a b c
2 a b c
3 a b c
my linq query 我的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
in my for i two fields, one for Value_A and Value_B 在我的两个字段中,一个用于Value_A和Value_B
on querying from table B to table A with the three colms. 用三个列从表B到表A进行查询。 i have to take distinct on col_A , col_B , col_C and DataAmt , the value from value col in TABLE_A which is greater than 1 should be placed in field Value_A and others in Value_B .
我必须采取不同的上为col_a,col_B,col_C和DataAmt,从在TABLE_A 值栏是大于1应放置在字段Value_A等人在Value_B值。
I cant take distinct values and the separation of values. 我不能接受不同的价值和价值的分离。
Maybe something like this, alter the select clause as you wish: 也许像这样,根据需要更改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();
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.