0

I want to update a column of a table which got ~4k rows. The column has to get the date of the parent so I'm using a simple foreach:

Parent.VariableDate = DateTime.Now;
foreach (var item in Parent.ChildList)
{
    item.vari = Parent.VariableDate ;

}

Problem:

This action is taking ages. Can I immediately update a whole column?

Konrad Kokosa
  • 15,790
  • 2
  • 33
  • 54
dennis schütz
  • 387
  • 3
  • 20
  • Don't know `WPF`, but does it has `BeginUpdate` or `SuspendLayout` analog? Try to avoid refreshing your changes somehow, possibly by creating the whole list of items unbound and then adding it to a control at once. – Sinatr Dec 10 '13 at 08:31

2 Answers2

0

you can use Linq for that

Parent.ChildList.foreach(p=>p.vari = DateTime.Now);

it is the fastest way

techloverr
  • 2,501
  • 1
  • 14
  • 28
  • Are you sure this will provide any better than a simple ForEach()? Seems to me the only difference here is some minor syntax changes.. – Kjartan Dec 10 '13 at 08:17
  • in fact, linq is not necessary faster. It executes deferred and can be faster than a "coded" solution. But to say "it is the fastest way" is way too absolute and plain wrong. see http://stackoverflow.com/questions/1044236/nested-foreach-vs-lambda-linq-query-performancelinq-to-objects for example. – Herm Dec 10 '13 at 08:31
  • @techloverr Have _you_ tried? :p I'm just asking for some justification for your claim. – Kjartan Dec 10 '13 at 08:31
0

I'm not sure you'll gain very much from writing this any other way. A better option, if possible in your situation(?), might be to avoid using VariableDate at all in each item in ChildList, and just refer to Parent instead whenever you need it. That way, you would not have to iterate through the 4K rows at all.

As presented in your question, each item.VariableDate seems to be superfluous..

Kjartan
  • 17,127
  • 14
  • 67
  • 84