Monday, August 20, 2007

First Experiences With VS2K8 Part 4

This is the fourth in a series of posts I'm going to be making about my experiences with Visual Studio 2008 and .NET3.5. You might also be interested in the other posts which are linked at the right. This is just a quick hit entry because I'm deep in the middle of my project now but I just figured something out that I want to share. One of the screens in my order taking application needs to allow the user to choose one or more orders from a list and then display those orders with all of their header information. The SQL equivalent would be something like: select * from OrderHeaders where OrderID in (list of user-selected ints) The trouble I ran into is that LINQ doesn't have an 'in' keyword. After a good deal of searching I finally figured out how to do it. LINQ doesn't have an 'in' keyword but it does have a 'Contains' method. Here is how you would use it: int[] orderIDs = getSelectedOrderIDsFromForm(); OrdersDataContext db = new OrdersDataContext(); var query = from oh in db.OrderHeaders where orderIDs.Contains(oh.OrderID) select oh; It's a little ass-backwards from the SQL way in that rather than in SQL we say we want OrderHeader records that contain the selected IDs. Here it sounds more like we're asking the list of IDs for matches instead. But using the Sql Profiler I see that the generated SQL is a normal query with an IN clause just like I would have written if I was writing the SQL myself. A little obtuse but pretty cool nonetheless.

No comments: