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");
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.
Perform the equivalent of an SQL IN with IEnumerable. Contains().
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.
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.
LINQ to SQL is a component of the . NET Framework that provides a run-time infrastructure for managing relational data as objects.
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.
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With