My first LINQ Experience. How to do a simple COUNT() with LINQ to SQL

So, nothing extraordinary here, but I’ve finally taken the dive into LINQ.  I’ve got a great book that helping me a lot that I recently review called Pro LINQ by Joseph Rattz (his humor is a little odd, but the book is great).  At anyrate, moving right a long, all I did was right click on my web projects app_code directory, choose LINQ to SQL classes,

appcode

Then, choose some tables from my database. Then, in my code, all I had to write was the class in my c# code (part of my database layer classes).  Here is what the code looks like (sorry about the jpg, I’m still trying to figure out how to get Copy SourceAsHTML working in vs2008)

linq1

And, without LINQ, this is what I would have had to do.  No type safety and took 3 times as long to write.

linq2

You can tell what I’ll be doing for now on (climbing the curve!)

About Peter Kellner

Follow me:


Comments

  1. Luisnet says:

    Thank you for the explanation…

  2. return (int)SqlHelper.ExecuteScalar(connectionString, “select count(*) from content where IsDirectory = 1 and id = @id”) == 0;

    That’s a single line.

    And does the LINQ implementation send a full query or just a count query? That can be a big waste, especially if this is called often.

    I would probably be caching this item as well.

Follow

Get every new post delivered to your Inbox

Join other followers: