SQL查询到LINQ C#[加入多个表]

我在我的sql server中处理这个查询select a.care_type_id, a.description,isChecked = case when b.care_type_id is null then false else true endfrom caretype aleft join patientinsurancetacitem b on a...

我在我的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.但是,我和操作符有问题.到目前为止我有这个代码;

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.

本文标题为:SQL查询到LINQ C#[加入多个表]

基础教程推荐