123 456 789 0
123 456 789 0

Reputation: 10865

ObservableCollection in C#

What's the best way to populate a collection with multiple data?

var query = (from p in obsDay where p.courseFaculty == "abellana" select p);

foreach (var item in query)
{
   facultyTime.Add(
      new TimePerDay() 
          { subjTime = 
              new ObservableCollection<TimeSpan> 
                  { item.courseTimeStart } 
          }
      ); 
}
availProf[randomNumber].actualTime = facultyTime;

I tried this one but it only adds one data on my collection? What am I doing wrong?

Upvotes: 0

Views: 322

Answers (3)

RayLoveless
RayLoveless

Reputation: 21108

Your query object probably only contains 1 item. try stepping through it with your debugger.

Upvotes: 0

McKay
McKay

Reputation: 12624

actualTime gets set as the facultyTime. Which is constantly getting Added to? I'd guess it's either something in the Add method, or something in the actualTime property setter that isn't going through the whole facultyTime collection (if indeed it is a collection). Some clarity on what these objects are would be helpful in determining the issue.

Upvotes: 0

Matt Hudson
Matt Hudson

Reputation: 7358

You should run the query from LinqPad to make sure that your data actually matches your expectations. Your foreach loop is fine, your query is only returning one result, which makes it a data issue.

Upvotes: 1

Related Questions