This is a migrated thread and some comments may be shown as answers.

Check if objects exist

5 Answers 128 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
sitefinitysteve asked on 22 Jan 2010, 11:56 PM
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?

5 Answers, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 25 Jan 2010, 03:34 PM
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.
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 25 Jan 2010, 03:41 PM
Can you check based on an entire object as well?...so not just the ID col, but like an object.Equals()?
0
Alexander
Telerik team
answered on 25 Jan 2010, 05:46 PM
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.
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 25 Jan 2010, 05:53 PM
Darn, okay thanks :)

Can you put in a feature request for some sort of IfExists(object) support?
0
Jordan
Telerik team
answered on 26 Jan 2010, 02:51 PM
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.
Tags
General Discussions
Asked by
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Answers by
Alexander
Telerik team
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Jordan
Telerik team
Share this question
or