我在sqlServer中有3个表如下:
Cost_Types
Id COST_NAME
-------------
1 A
2 B
3 C
4 D
5 E
6 F
7 Z
和请求表
Id No
----------
1 100
2 200
3 300
和 Cost_Request (=clearance_cost)
RequestId CostId Amount
-------------------------------
1 2 200
1 3 400
我想得到这个结果
RequestId CostId Amount
-------------------------------
1 2 200
1 3 400
1 1 0
1 4 0
1 5 0
1 6 0
我写这段代码
var context = new CLEARANCEEntities();
var items = (from c in context.COST_TYPES
join t in context.CLEARANCE_COST
on c.COST_ID equals t.COST_ID into outer
from t in outer.DefaultIfEmpty()
where t. RequestId==1
select new
{
c.COST_ID,
c.COST_NAME, I = ((t == null) ? 0 : t.COST_AMOUNT)
}).ToList();
这段代码只返回这个
RequestId CostId Amount
-------------------------------
1 2 200
1 3 400
我想如果改变查询如下:
var context = new CLEARANCEEntities();
var items = (from c in context.COST_TYPES
join t in context.CLEARANCE_COST
on c.COST_ID equals t.COST_ID into outer
from t in outer.DefaultIfEmpty()
where t.RequestId==2
select new
{
c.COST_ID,
c.COST_NAME, I = ((t == null) ? 0 : t.COST_AMOUNT)
}).ToList();
返回此数据
RequestId CostId Amount
-------------------------------
2 1 0
2 2 0
2 3 0
2 4 0
2 5 0
2 6 0
请帮我。谢谢大家