GetChangeSet () request to search for a specific object?

Providing the following code:

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

Suppose the previous code is in a method that is called several times. I do not want to represent the same object twice. Can I query DataContextwith help GetChangeSet()to see if this object exists in ChangeSet?

GetChangeSet().Insertreturns IList<object>I draw a space on how to find it.

+3
source share
2 answers

Figured it out ... Using the question code

ctx.GetChangeSet().Inserts.Any(ani => ani as Animal != null 
                                   && ((Animal) ani).Name == a.Name); 
+3
source

Instead, you can use the extension method OfType<T>(). Little cleaner.

ctx.GetChangeSet().Inserts
.OfType<Animal>().Any(ani => ani.Name == a.Name);
+1
source

Source: https://habr.com/ru/post/1759219/


All Articles