LINQ to Entites: How should I handle System.InvalidOperationException when checking for existance of

Posted by chris on Stack Overflow See other posts from Stack Overflow or by chris
Published on 2010-04-14T19:13:06Z Indexed on 2010/04/14 19:23 UTC
Read the original article Hit count: 204

I have a many-to-one relationship that users can edit via checkboxes. PK of Foo is ID, and fid contains the id from the checkbox.

I'm checking to see if an element exists with:

Foo ent;
try
{
  ent = ctx.Foo.First(f => f.ID == fid);
}
catch (System.InvalidOperationException ioe)
{
  ent = new Foo();
}

It seems to me that I should be able to do this without throwing an exception. What would be the best way to do this?

© Stack Overflow or respective owner

Related posts about linq-to-entities

Related posts about exception-handling