Check if objects exist

Thread is closed for posting
6 posts, 0 answers
  1. Steve
    Steve avatar
    1886 posts
    Member since:
    Dec 2008

    Posted 22 Jan 2010 Link to this post

    Whats the best way to check if an object already exists in the database?  If a user refreshes my form it tries to re-insert the same object back into my table and I get an exception...

    Is there a best practice for that?

  2. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 25 Jan 2010 Link to this post

    Hello Steve,

    I am afraid you will have to execute a query to determine if an object with particular ID already exists. You can use the FirstOrDefault() method:
    Customer c = scope.Extent<Customer>().FirstOrDefault(x => x.CustomerID == "ALFKI");
    The method returns null if the object is not found. Hope that helps.

    Greetings,
    Alexander
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Steve
    Steve avatar
    1886 posts
    Member since:
    Dec 2008

    Posted 25 Jan 2010 Link to this post

    Can you check based on an entire object as well?...so not just the ID col, but like an object.Equals()?
  4. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 25 Jan 2010 Link to this post

    Hello Steve,

    Once you have retrieved the object with the query mentioned in the previous post you can compare it manually to the other object property by property or by implementing a Comparer class that can do this for you.

    Kind regards,
    Alexander
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. Steve
    Steve avatar
    1886 posts
    Member since:
    Dec 2008

    Posted 25 Jan 2010 Link to this post

    Darn, okay thanks :)

    Can you put in a feature request for some sort of IfExists(object) support?
  6. Jordan
    Admin
    Jordan avatar
    547 posts

    Posted 26 Jan 2010 Link to this post

    Hi Steve,

    Sure, we will add this feature to our TODO list.
    Unfortunately I cannot give you an exact time frame for when it will be implemented at the moment.

    Greetings,
    Jordan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top