10

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?

3 Answers 3

3

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();
Sign up to request clarification or add additional context in comments.

7 Comments

it should be the opposite logic in the first example FirstOrDefault() == null and
getting - A query body must end with a select clause or a group clause
yeah my fault i fixed it ty @AlexArt.
thanks working fine on linqPAD but inside my c# app it is fetching all records :( why ?
.FirstOrDefault().Any() - You don't need to do .FirstOrDefault() before Any() it needs to be just .Any()
|
2

I think your original query is fine you just need to add another pair of brackets in the where clause:

(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();

2 Comments

I think issue is I'm taking table values inside var variable thats why may be it is wrong? any clue ? I'm getting all localizationKey values as a output
Then most probably the issue is with your sql query. I assume that it returns the same result
1

Try 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 1).FirstOrDefault()   

 select lk).ToList();

2 Comments

Cannot implicitly convert type 'int' to 'bool'
I think issue is I'm taking table values inside var variable thats why may be it is wrong? any clue ? I'm getting all localizationKey values as a output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.