Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is SQL equivalent to LINQ .All()

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

// where people is a list of Person objects with property Name
bool bobs = people.All(p => p.Name == "Bob");
like image 832
d456 Avatar asked Jan 29 '15 18:01

d456


People also ask

What is LINQ in SQL?

LINQ to SQL is a component of . NET Framework version 3.5 that provides a run-time infrastructure for managing relational data as objects. Note. Relational data appears as a collection of two-dimensional tables (relations or flat files), where common columns relate tables to each other.

What is the LINQ equivalent to the SQL IN operator?

Perform the equivalent of an SQL IN with IEnumerable. Contains().

Is LINQ to SQL still used?

LINQ to SQL was the first object-relational mapping technology released by Microsoft. It works well in basic scenarios and continues to be supported in Visual Studio, but it's no longer under active development.

Is LINQ converted to SQL?

LINQ to SQL translates the queries you write into equivalent SQL queries and sends them to the server for processing. More specifically, your application uses the LINQ to SQL API to request query execution. The LINQ to SQL provider then transforms the query into SQL text and delegates execution to the ADO provider.

What is LINQ to SQL classes?

LINQ to SQL is a component of the . NET Framework that provides a run-time infrastructure for managing relational data as objects.

Is LINQ or SQL faster?

We can see right away that LINQ is a lot slower than raw SQL, but compiled LINQ is a bit faster. Note that results are in microseconds; real-world queries may take tens or even hundreds of milliseconds, so LINQ overhead will be hardly noticeable.


2 Answers

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:

not exists(select * from Persons where Name <> 'Bob' or Name is null)
like image 105
Guffa Avatar answered Sep 19 '22 04:09

Guffa


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

'Bob' = ALL (SELECT name FROM people)
like image 39
ypercubeᵀᴹ Avatar answered Sep 18 '22 04:09

ypercubeᵀᴹ