background preloader

LINQ

Facebook Twitter

Creating IN Queries With Linq To Sql. Props on this one go to Scott Hanselman who pulled me back from the edge of the cliff last night. I was particularly distraught in getting a MIX demo together where I had to do some queries using LINQ, and I couldn’t for the life of me figure out how to fashion an IN query! With Scott’s help (and patience) I figured it out, and thought I should blog for my own reference, at least. It Depends On What Your Definition of “IN” Is… An IN query will pull back a set of results from SQL that is within a given range. SELECT * FROM Production.Product WHERE ProductID IN (SELECT ProductID FROM Sales.ShoppingCartItem WHERE ShoppingCartID='RobsCart') This will return all the Product records that are in my cart.

LINQ Is People To (Or It’s Made From People…) It’s important to remember that the people that made LINQ were trying to approximate a “SQL within Code” sort of thing - this means that they built LINQ to query just about anything, and also built a SQL Translator called Linq To Sql. SELECT [t0]. Using LINQ to SQL (Part 1) Over the last few months I wrote a series of blog posts that covered some of the new language features that are coming with the Visual Studio and .NET Framework "Orcas" release.

Here are pointers to the posts in my series: The above language features help make querying data a first class programming concept. We call this overall querying programming model "LINQ" - which stands for .NET Language Integrated Query. Developers can use LINQ with any data source. They can express efficient query behavior in their programming language of choice, optionally transform/shape data query results into whatever format they want, and then easily manipulate the results. LINQ-enabled languages can provide full type-safety and compile-time checking of query expressions, and development tools can provide full intellisense, debugging, and rich refactoring support when writing LINQ code.

What Is LINQ to SQL? LINQ to SQL fully supports transactions, views, and stored procedures. LINQ to SQL Code Examples Summary. LINQ to SQL (Part 2 - Defining our Data Model Classes) In Part 1 of my LINQ to SQL blog post series I discussed "What is LINQ to SQL" and provided a basic overview of some of the data scenarios it enables. In my first post I provided code samples that demonstrated how to perform common data scenarios using LINQ to SQL including: How to query a database How to update rows in a database How to insert and relate multiple rows in a database How to delete rows in a database How to call a stored procedure How to retrieve data with server-side paging I performed all of these data scenarios using a LINQ to SQL class model that looked like the one below: In this second blog post in the series I'm going to go into more detail on how to create the above LINQ to SQL data model.

LINQ to SQL, the LINQ to SQL Designer, and all of the features that I'm covering in this blog post series will ship as part of the .NET 3.5 and Visual Studio "Orcas" release. Create a New LINQ to SQL Data Model Entity Classes Creating Entity Classes From a Database Delay/Lazy Loading. LINQ to SQL (Part 3 - Querying our Database) Last month I started a blog post series covering LINQ to SQL. LINQ to SQL is a built-in O/RM (object relational mapping) framework that ships in the .NET Framework 3.5 release, and which enables you to easily model relational databases using .NET classes. You can then use LINQ expressions to query the database with them, as well as update/insert/delete data from it. Below are the first two parts of my LINQ to SQL series: In today's blog post I'll be going into more detail on how to use the data model we created in the Part 2 post, and show how to use it to query data within an ASP.NET project.

Northwind Database Modeled using LINQ to SQL In Part 2 of this series I walked through how to create a LINQ to SQL class model using the LINQ to SQL designer that is built-into VS 2008. Retrieving Products Once we have defined our data model classes above, we can easily query and retrieve data from our database. Visualizing LINQ to SQL Queries in the Debugger Shaping our Query Results Summary Scott. LINQ to SQL (Part 4 - Updating our Database) Over the last few weeks I've been writing a series of blog posts that cover LINQ to SQL. LINQ to SQL is a built-in O/RM (object relational mapper) that ships in the .NET Framework 3.5 release, and which enables you to easily model relational databases using .NET classes.

You can use LINQ expressions to query the database with them, as well as update/insert/delete data. Below are the first three parts of my LINQ to SQL series: In today's blog post I'll cover how we we can use the data model we created earlier, and use it to update, insert, and delete data. I'll also show how we can cleanly integrate business rules and custom validation logic with our data model. Northwind Database Modeled using LINQ to SQL In Part 2 of this series I walked through how to create a LINQ to SQL class model using the LINQ to SQL designer that is built-into VS 2008. For example, I could write the below LINQ expression to retrieve a single Product object by searching on the Product name: Insert and Delete Examples. LINQ to SQL (Part 5 - Binding UI using the ASP:LinqDataSource Control) Over the last few weeks I've been writing a series of blog posts that cover LINQ to SQL.

LINQ to SQL is a built-in O/RM (object relational mapper) that ships in the .NET Framework 3.5 release, and which enables you to easily model relational databases using .NET classes. You can use LINQ expressions to query the database with them, as well as update/insert/delete data. Below are the first four parts of my LINQ to SQL series: In these previous LINQ to SQL blog posts I focused on how you can programmatically use LINQ to SQL to easily query and update data within a database. In today's blog post I'll cover the new <asp:LinqDataSource> control that is shipping as part of ASP.NET in the upcoming .NET 3.5 release. This control is a new datasource control for ASP.NET (like the ObjectDataSource and SQLDataSource controls that shipped with ASP.NET 2.0) which makes declaratively binding ASP.NET UI controls to LINQ to SQL data models super easy. Sample Application We'll be Building Summary. LINQ to SQL (Part 6 - Retrieving Data Using Stored Procedures)

Over the last few weeks I've been writing a series of blog posts that cover LINQ to SQL. LINQ to SQL is a built-in O/RM (object relational mapper) that ships in the .NET Framework 3.5 release, and which enables you to model relational databases using .NET classes. You can use LINQ expressions to query the database with them, as well as update/insert/delete data. Below are the first five parts of my LINQ to SQL series: In these previous LINQ to SQL blog posts I demonstrated how you could use LINQ query expressions to programmatically retrieve data from a database. In today's blog post I'll cover how you can also use database stored procedures (SPROCs) and user defined functions (UDFs) with your LINQ to SQL data model. To SPROC or not to SPROC? The question of whether to use Dynamic SQL generated by an ORM or instead use Stored Procedures when building a data layer is a topic that generates endless (very passionate) debate amongst developers, architects and DBAs.

Calling the SPROC in VB: LINQ to SQL (Part 7 - Updating our Database using Stored Procedures) Over the last few weeks I've been writing a series of blog posts that cover LINQ to SQL. LINQ to SQL is a built-in O/RM (object relational mapper) that ships in the .NET Framework 3.5 release, and which enables you to model relational databases using .NET classes. You can use LINQ expressions to query the database with them, as well as update/insert/delete data. Below are the first six parts in this series: In part 6 I demonstrated how you can optionally use database stored procedures (SPROCs) and user defined functions (UDFs) to query and retrieve data using your LINQ to SQL data model. In today's blog post I'm going to discuss how you can also optionally use SPROCs to update/insert/delete data from the database. To help illustrate this - let's start from scratch and build-up a data access layer for the Northwind sample database: Step 1: Creating our Data Access Layer (without using SPROCs yet) Adding Validation Rules to our Data Model Classes or: Step 3: Doing Order Inserts with a SPROC.

LINQ to SQL (Part 8 - Executing Custom SQL Expressions) Over the last few weeks I've been writing a series of blog posts that cover LINQ to SQL. LINQ to SQL is a built-in O/RM (object relational mapper) that ships in the .NET Framework 3.5 release, and which enables you to model relational databases using .NET classes. You can use LINQ expressions to query the database with them, as well as update/insert/delete data.

Below are the first seven parts in this series: In my last two posts (Part 6 and Part 7) I demonstrated how you can optionally use database stored procedures (SPROCs) to query, insert, update and delete data using a LINQ to SQL data model. One of the questions a few people have asked me since doing these posts has been "what if I want total control over the SQL expressions used by LINQ to SQL - but I don't want to use SPROCs to-do it? " Using LINQ Query Expressions with LINQ to SQL Using Custom SQL Queries with LINQ to SQL Using the ExecuteQuery Method Custom SQL Expressions and Object Tracking for Updates Summary Hope this helps, Scott.

LINQ to SQL (Part 9 - Using a Custom LINQ Expression with the <asp:LinqDatasource> control) Over the last few weeks I've been writing a series of blog posts that cover LINQ to SQL. LINQ to SQL is a built-in O/RM (object relational mapper) that ships in the .NET Framework 3.5 release, and which enables you to model relational databases using .NET classes. You can use LINQ expressions to query the database with them, as well as update/insert/delete data.

Below are the first eight parts in this series: In Part 5 of the series I introduced the new <asp:LinqDataSource> control in .NET 3.5 and talked about how you can use it to easily bind ASP.NET UI controls to LINQ to SQL data models. In both of these articles the queries I performed were relatively straight-forward (the where clause worked against a single table of data). Quick Recap: <asp:LinqDataSource> with a Declarative Where Statement In these two posts I demonstrated how you can use the built-in filter capabilities of the LinqDataSource control to declaratively express a filter statement on a LINQ to SQL data model. Summary.