Tigraine
Daniel Hoelbling talks about programming

Strange LinQ to SQL oddity

February 18th, 2008 . by Daniel Hölbling

I’m still in the process of doing some research on the tools I’m going to use over the next weeks to build the Pizza.at web app.
So today I tried some LinQ to SQL so I can get used to it a bit. And while doing so I started out with reading through ScottGu’s LinQ to SQL series.

I had already some parts of the database schema in place and so I imported it to the ORM, giving me the following model:

linq1

So, this all worked perfectly and I immediately started doing some select statements (while learning how to use and write Lambda expressions – I’ll blog about this later).

linq2

And yippie! It worked, although I was very confused that this statement would return a InvalidOperationException in case the Lambda didn’t return something. Although I like the fact that it returns an exception, I think the exception given here is too generic. They could have done better in giving the exception a stronger name.

But, after getting over the exception name, everything went smooth from there. Except for me not seeing one thing:
The C and D in CRUD!

ScottGu showed off in his series that doing a simple Categories.Add and an afterwards call to dbContext.SubmitChanges() should do.
In my version of Visual Studio and LinQ I simply don’t find this method. All lists I get from the ORM are IQueryable and don’t incorporate any Add and Delete Methods.

I’m getting a bit confused, I must be missing something at this point, there must be something I have overlooked. Any thoughts?


  • http://www.chrisjsmith.me.uk/ Chris

    Try using dbContext.Categories.SingleOrDefault(s=>s…..);

    You will get a null then.

  • http://www.tigraine.at Tigraine

    Thanks for the comment!
    I have been using SingleOrDefaults from time to time since, but never thought about using it to do error correction.