gpt4 book ai didi

c# - SQL 查询到 LINQ C# [连接多个表]

转载 作者:太空狗 更新时间:2023-10-30 00:38:47 26 4
gpt4 key购买 nike

我正在我的 sql server 中处理这个查询

select a.care_type_id, a.description,
isChecked = case when b.care_type_id is null then 'false' else 'true' end
from caretype a
left join patientinsurancetacitem b on a.care_type_id = b.care_type_id and
b.tac_id = 1

我想将查询转换为 LINQ。但是,我在使用 and 运算符时遇到了问题。到目前为止我有这段代码;

from a in context.CareTypes
join b in context.PatientInsuranceTACItems on a.care_type_id equals
b.care_type_id into x
from xx in x.Where(w => w.tac_id == 1).DefaultIfEmpty()
select new {
isChecked = (b.care_type_id == null ? false : true),
care_type_id = a.care_type_id,
description = a.description}

而且,我无法获得在 isChecked 变量中等同的 b。我将从哪里开始修改以获得与我的 sql 查询相同的结果?我哪里弄错了?

最佳答案

试试这个

from a in context.caretype
join b on context.patientinsurancetacitem
on new { CA = a.care_type_id, CB = 1} equals
new { CA = b.care_type_id, CB = b.tac_id}
into tmp from b in tmp.DefaultIfEmpty()
select new
{
care_type_id = a.care_type_id,
description = a.description,
checked = (b != null) // Or ((b == null) ? false : true)
}

同时检查 this StackOverflow answer .

关于c# - SQL 查询到 LINQ C# [连接多个表],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38323394/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com