我有以下查询
select distinct (case
when object like'%service%' then (object)
when host like '%service%' then (host)
when class like '%service%' then (class)
when parameter like '%service%' then (parameter)
end), (case
when object like'%service%' then 'objet'
when class like '%service%' then 'objet_class'
when host like '%service%' then 'host'
when parameter like '%service%' then 'parametre'
end) from [dbo].[full] where(object <>'NULL' and class<>'NULL' and parameter<>'NULL' and host <>'NULL')
我想知道如何在实体框架中编写linq查询。
答案 0 :(得分:1)
.Contains("x")
应翻译为LIKE '%x%'
。
? :
应翻译为CASE WHEN
。
var q = from f in dbContext.Full
where !f.object.Equals("NULL") && !f.class.Equals("NULL") && !f.parameter.Equals("NULL") && !f.host.Equals("NULL")
select new {
f.object.Contains("service") ? f.object :
f.host.Contains("service") ? f.host :
f.class.Contains("service") ? f.class :
f.parameter.Contains("service") ? f.parameter :
null,
f.object.Contains("service") ? "object" :
f.host.Contains("service") ? "host" :
f.class.Contains("service") ? "object_class" :
f.parameter.Contains("service") ? "parameter" :
"N/A"
};
答案 1 :(得分:1)
试试这个:
var temp =
from f in context.full
where f.object != "NULL" && f.class != "NULL" && f.parameter != "NULL"
select new {
service = f.object.Contains("service") ? f.object :
f.host.Contains("service") ? f.host :
f.class.Contains("service") ? f.class :
f.parameter.Contains("service") ? f.parameter,
serviceType = f.object.Contains("service") ? "object" :
f.host.Contains("service") ? "host" :
f.class.Contains("service") ? "object_class" :
f.parameter.Contains("service") ? "parameter"
}