Morgs
Morgs

Reputation: 1746

Rewrite Entity Framework LINQ query into T-SQL

How can I rewrite the following Linq query into normal T-SQL?

var list = (from t in context.ParentTable
            where t.ChildRecords.Count == t.ChildRecord.Count( c => c.BooleanColumn )
            select t).ToList();

Thanks in advance...

Upvotes: 1

Views: 201

Answers (1)

juharr
juharr

Reputation: 32286

Something like this, but you'll need to determine the relationship between the ParentTable and ChildRecord tables to make it work, I'm just guessing at the cr.ParentTableId = pt.ParentTableId part.

select pt.*
from ParentTable pt
where not exists 
(select 1 
 from ChildRecord cr 
 where cr.ParentTableId = pt.ParentTableId
       and cr.BooleanColumn = 0)

On a side note the Linq could be changed to the following instead.

var list = (from t in context.ParentTable
            where t.ChildRecords.All(c => c.BooleanColumn)
            select t).ToList();

Upvotes: 1

Related Questions