什么是SQL等效于LINQ .All()

时间:2022-08-09 15:46:45

What would this look like in SQL (SQL Server if you want to be particular)?

这在SQL中会是什么样子(SQL Server,如果你想特别的话)?

// where people is a list of Person objects with property Name
bool bobs = people.All(p => p.Name == "Bob");

2 个解决方案

#1


5  

You would check if there are any records that doesn't match the criteria:

您将检查是否有任何记录与条件不符:

not exists(select * from Persons where not Name = 'Bob')

As the rules for comparing to null are different between C# and SQL, you would need a condition for null values if the field allows them:

由于C#和SQL之间的比较规则不同,如果字段允许,则需要一个空值条件:

not exists(select * from Persons where Name <> 'Bob' or Name is null)

#2


3  

I'm not sure what query exactly Linq will create but the equivalent in SQL is the ALL operator:

我不确定究竟Linq将创建什么查询,但SQL中的等价物是ALL运算符:

'Bob' = ALL (SELECT name FROM people)

#1


5  

You would check if there are any records that doesn't match the criteria:

您将检查是否有任何记录与条件不符:

not exists(select * from Persons where not Name = 'Bob')

As the rules for comparing to null are different between C# and SQL, you would need a condition for null values if the field allows them:

由于C#和SQL之间的比较规则不同,如果字段允许,则需要一个空值条件:

not exists(select * from Persons where Name <> 'Bob' or Name is null)

#2


3  

I'm not sure what query exactly Linq will create but the equivalent in SQL is the ALL operator:

我不确定究竟Linq将创建什么查询,但SQL中的等价物是ALL运算符:

'Bob' = ALL (SELECT name FROM people)