SQL Query to LINQ syntax using not exist and join
My SQL query is like below working fine in SQL I need to convert this to LINQ syntax
SQL-
SELECT [Key], Id
FROM LocalizationKeys AS lk
WHERE NOT EXISTS (SELECT 1
FROM Languages AS l
JOIN LocalizationValues AS lv ON l.Id = lv.LanguageId
WHERE l.Title = 'en-US' AND lv.LocalizationKeyId = lk.Id)
LINQ syntax I tried
var result =
(from lk in localizationKey
where !(from l in lang
join lv in localizationValue on l.Id equals lv.LanguageId
where l.Title == "en-US" && lv.LocalizationKeyId == lk.Id select 1).FirstOrDefault()
select lk).ToList();
Getting error:
Operator '!' cannot be applied to operand of type 'int'
Any clue where I made mistake?
You can try like this:
(from lk in localizationKey
where (from l in lang
join lv in localizationValue on l.Id equals lv.LanguageId
where (l.Title == "en-US" && lv.LocalizationKeyId == lk.Id)
select l).FirstOrDefault() == null
select lk).ToList();
or
(from lk in localizationKey
where !(from l in lang
join lv in localizationValue on l.Id equals lv.LanguageId
where !(l.Title == "en-US" && lv.LocalizationKeyId == lk.Id)
select l).FirstOrDefault().Any()
select lk).ToList();
尝试这个:
(from lk in localizationKey
where (from l in lang
join lv in localizationValue on l.Id equals lv.LanguageId
where !(l.Title == "en-US" && lv.LocalizationKeyId == lk.Id) select 1).FirstOrDefault()
select lk).ToList();
我认为你原来的查询很好,你只需要在where子句中添加另一对括号:
(from lk in localizationKey
where !((from l in lang
join lv in localizationValue on l.Id equals lv.LanguageId
where l.Title == "en-US" && lv.LocalizationKeyId == lk.Id select 1).Any())
select lk).ToList();
链接地址: http://www.djcxy.com/p/32994.html
上一篇: 如何查询逗号分隔的字符串?