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.

Creating IN Queries With Linq To Sql

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. This range can be set manually, or can itself be a query. 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.

Var itemQuery = from cartItems in db.SalesOrderDetails where cartItems.SalesOrderID == 75144 select cartItems.ProductID; Next we need to get the products, but only those that are in the cart. Var myProducts = from p in db.Products Hey! 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.

Using LINQ to SQL (Part 1)

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 supports a very rich extensibility model that facilitates the creation of very efficient domain-specific operators for data sources.

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.

LINQ to SQL (Part 2 - Defining our Data Model Classes)

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.

LINQ to SQL (Part 3 - Querying our Database) Last month I started a blog post series covering LINQ to SQL.

LINQ to SQL (Part 3 - Querying our Database)

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. 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 (Part 4 - Updating our Database)

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. LINQ to SQL (Part 5 - Binding UI using the ASP:LinqDataSource Control) LINQ to SQL (Part 6 - Retrieving Data Using Stored Procedures) 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 (Part 7 - Updating our Database using Stored Procedures)

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: LINQ to SQL (Part 8 - Executing Custom SQL Expressions) 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 (Part 9 - Using a Custom LINQ Expression with the <asp:LinqDatasource> control)

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. I also demonstrated how to use it a little more in a follow-up post I did that discusses the new <asp:ListView> control (Part 1 - Building a Product Listing Page with Clean CSS UI). 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 Summary Scott.