0

I was wondering if there is a smarter/faster/one_line way to approach this.

I have a list of dll's with IsActive property. On the UI I have checkboxes which change state of dll's depending on whether the checkbox is checked or not. Change happens on button click. I have no problem traversing the whole DB so now I am doing it like this:

foreach (var item in dllList)
{
    context.dllSet.Find(item.Id).IsActive = item.IsActive;
}

(dllList is List<> element connected to WPF front).

So now I am finding element with same id in DB, and changing its state to one that is in presented in UI checkbox.

Solution I'd like would be something like this:

context.dllSet.AddOrUpdateList(dllList);
Norgul
  • 4,613
  • 13
  • 61
  • 144
  • 1
    might be usefull http://stackoverflow.com/questions/11421370/efficient-way-of-updating-list-of-entities – Vladimir Dec 01 '16 at 10:08
  • 1
    How about: context.dllSet.Where(a=>dllList.Contains(a.Id)).ToList().ForEach(a=>a.IsActive = dllList.Find(a.Id).IsActive); – Marcus Höglund Dec 01 '16 at 10:12
  • Not as elegant as I imagined...but working with few modifications :) – Norgul Dec 01 '16 at 11:29
  • It depends on the way you transfer data betweem DB and UI. Lets say you bind your visible list directly to the list of database entries and you keep your context alive, then the result is as easy as calling `SaveChanges` on click. If you design your click handler in a smart way you will know which entry was changed and you don't have to update the whole list anyway. If your `dllList` contains the same entity type as the `dllSet`, you can attach the items to a new context and mark `IsActive` as modified like in the link provided by Vladimir. You have many options and don't give alot information – grek40 Dec 01 '16 at 12:42

1 Answers1

0

Create Stored procedure with Datatabel as a parameter and implement the logic in DB end.

Call your stored procedure from entity framework.

Vijay
  • 663
  • 4
  • 15