I am working on this query in my sql server
我正在我的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
I want to translate the query into LINQ. However, I am having trouble with the and
operator. I have this code so far;
我想将查询转换为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}
And, also, I cannot get the b
that I equated in isChecked
variable. From where will I start modifying in order to get the same result as my sql query? In where I got it wrong?
而且,我也无法得到我在isChecked变量中等同的b。从哪里开始修改以获得与我的sql查询相同的结果?在哪里弄错了?
2 个解决方案
#1
5
Try this
尝试这个
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)
}
Also check this * answer.
另请检查此*答案。
#2
2
The very simple example about joining on multiple columns is ;
关于连接多个列的一个非常简单的例子是:
from x in entity1
join y in entity2
on new { X1= x.field1, X2= x.field2 } equals new { X1=y.field1, X2= y.field2 }
#1
5
Try this
尝试这个
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)
}
Also check this * answer.
另请检查此*答案。
#2
2
The very simple example about joining on multiple columns is ;
关于连接多个列的一个非常简单的例子是:
from x in entity1
join y in entity2
on new { X1= x.field1, X2= x.field2 } equals new { X1=y.field1, X2= y.field2 }