Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 17th, 2012  |  syntax: None  |  size: 0.40 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to check-not add duplicates with EF from a list
  2. foreach (var item in so)
  3. {
  4.        context.ORDERS.AddObject(item);
  5. }
  6. db.SaveChanges();
  7.        
  8. foreach (var item in so)
  9. {
  10.     //If ORDERS List doesn't contain any item with the same id
  11.     //or any other predicate match
  12.     if(!context.ORDERS.Any(o => o.Id == item.Id)) //or any other property of item
  13.        context.ORDERS.AddObject(item);
  14. }
  15. db.SaveChanges();