Using "Like" in LINQ to SQL

I’ve recently discovered that LINQ and VB can be confusing. I was attempting to pull the top 10 rows of a table where the column started with ‘W’. You’d think after I typed that, I’d figure out the solution. That’s not the case. Instead, I struggled. Here’s what I originally came up with: Dim q = From t In db.Employees Where t.LastName Like prefixText & "%" Select t.LastName Take count Little did I realize that the Like operator in my query was the VB Like operator. So, I had to modify my lambda expression to this: Dim q = From … Continue reading Using "Like" in LINQ to SQL

Using LINQ to SQL and ConnectionStrings in a Class Library

I was looking to separate my LINQ to SQL data layer from the rest of my application. So, I decided to move the files to a class library. This way, when my application builds, all of my LINQ files are found in MyApp.DL or something similar. However, I wanted the class library to use the SQL connection that I have set in my web.config. Because the class library does not have a reference to System.Configuration, I had to add that first. I then created a partial class for my Context object. Inside, I created a new constructor with a boolean … Continue reading Using LINQ to SQL and ConnectionStrings in a Class Library

Obtain the Identity of a New Row Using LINQ

It’s actually pretty easy and user friendly to obtain the value of the identity column using LINQ. After you create your object and insert it on submit, you can call the identity column’s property on your object. For instance: Dim db As New BlogDataContext() db.BlogPosts.InsertOnSubmit(MyPost) db.SubmitChanges() Dim IdentityValue As Integer = MyPost.PostID In this example, the IdentityValue variable would be assigned to the PostID of the post that was just submitted.

Bad SQL Mistake For One Forum Goer

This isn’t funny, but it is. It should be a daily WTF. Wally just sent me this forum post where someone deleted a little more than anticipated. It sounds like this guy can be in quite a lot of trouble… http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1263379&SiteID=1. I’d recommend setting up backups on your SQL server next time.

Aggregate Functions in LINQ

I found a great post which summarizes aggregate functions in LINQ. It includes examples for Average, Sum, and Count. Check it out at http://www.dev102.com/2008/04/16/how-to-use-aggregate-functions-with-linq-to-sql/. As always, you can test your functions by using LINQPad which is available at http://www.linqpad.net/.

Generate a PDF behind the scenes using SQL Reporting Services

I came across this really cool demonstration and article by Simon Pang earlier today. I asked around awhile ago for a solution where I could create a PDF on the fly using SQL Reporting Services. Cool enough, he came up with a solution. Check out his article on the Code Project at http://www.codeproject.com/useritems/ssrs_pdf.asp.