Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query GetChangeSet() to find a specific object?

Tags:

linq-to-sql

Giving the following code:

Animal a = new Animal { Name = "Rover", Type = "Dog" };
ctx.Animal.InsertOnSubmit(a);

Lets say the preceding code is in a method that gets called multiple times. I do not want to submit the same object twice. Would it be possible to query the DataContext using GetChangeSet() to see if this object already exists in the ChangeSet?

GetChangeSet().Insert returns an IList<object> I am drawing a blank as to how to find it.

like image 868
Mike Fielden Avatar asked Apr 07 '26 23:04

Mike Fielden


1 Answers

Figured it out... Using the question's code

ctx.GetChangeSet().Inserts.Any(ani => ani as Animal != null 
                                   && ((Animal) ani).Name == a.Name); 
like image 76
Mike Fielden Avatar answered Apr 14 '26 00:04

Mike Fielden