我有两个相关的表,billing_transactions和billing_transaction_accessorial_charge。第二个是多个第一个。
第二个表中只有两种类型的记录,我需要在查询中知道它是否有,一个或两个。类型是service_code_id = 1(意味着此交付是住宅交付)或2(意味着它有一台洗碗机) - 交付可以同时具有。
我已尝试过以下操作,但如果有两个条目(显然),它会返回两条记录。
我假设我可以使用Lambda表达式执行此操作,但到目前为止我已尝试过多种编写方式。我使用我能想到的每个搜索词进行搜索...我知道这可能是一个重复...但我找不到它。
这里只是将其缩小到一些我知道有相关项目的记录。
var gridData = (from d in db.billing_transactions
join a in db.billing_transaction_accessorial_charge on d.Id equals a.billing_transaction_id into Inners
from sd in Inners.DefaultIfEmpty()
where (d.Id > 954 && d.Id < 958)
select new
{
d.base_amount,
d.Id,
residential = sd.service_code_id == 1 ? "Res" : "no res" ,
dishMachine = sd.service_code_id == 2 ? "Dish" : "No dish",
drivers_name = d.stop_details.driver_details.first_name + " " + d.stop_details.driver_details.last_name,
placeholder1 = "",
select_col = 0
}).Distinct().ToArray();
这就是数据的样子:
billing_transaction : Id - 99
billing_transaction_accessorial_charge (record1) billing_transaction_id - 99, service_code_id - 1
billing_transaction_accessorial_charge (record2) billing_transaction_id - 99, service_code_id - 2
返回的数据集类似于:
Id=99 residential = Res, dishMachine = Dish
或者,如果没有service_code_id为2的记录则会有dishMachine =“no dish”
答案 0 :(得分:0)
Aluan向我指出了正确的方向 - 谢谢你。
以下是我需要的:
var gridData = (from d in db.billing_transactions
where (d.Id > 954 && d.Id < 958)
select new
{
d.base_amount,
d.Id,
dishmachine = d.billing_transaction_accessorial_charge.Where (p => p.service_code_id == 2),
residential = d.billing_transaction_accessorial_charge.Where(p => p.service_code_id == 1),
drivers_name = d.stop_details.driver_details.first_name + " " + d.stop_details.driver_details.last_name,
placeholder1 = "",
select_col = 0
}
).ToArray();