-1
foreach (int i in temp)
    data.Add(i);

where temp is a List and data is an ObservableCollection

Cœur
  • 32,421
  • 21
  • 173
  • 232

4 Answers4

1

Why don't you just do

var data = new ObservableCollection(temp);
Abdul Munim
  • 17,662
  • 7
  • 48
  • 59
0
temp.ForEach(x => data.Add(x));
Roy Dictus
  • 30,703
  • 5
  • 56
  • 69
0
// if data is empty just pass temp in the constructor
ObservableCollection<int> data = new ObservableCollection<int>(temp);

// if data already has values you can do this using List.ForEach method
// but this would not be a LINQ since LINQ not able to modify data sources itself
temp.ForEach(i => data.Add(i));
sll
  • 56,967
  • 21
  • 100
  • 149
  • any other possibilities other than this?i dont want to use foreach as it has the same behaviour of "foreach" statement – hari venkatesh Dec 07 '11 at 12:09
  • What exacly you want to achieve? I'm a bit confused by a lambda requirement, you need to pass this as delegate to some method or just make a code block shorter? If latter - just use `foreach` loop as in your question – sll Dec 07 '11 at 12:11
0

Not really anything wrong with what you got. ObservableCollection is a bit of a pain tho.

Here are some useful extension methods that add things like an 'AddRange' method for it.

http://zainshaikh.posterous.com/some-extension-methods-for-observablecollecti

Dave Walker
  • 3,358
  • 1
  • 21
  • 25