views:

415

answers:

1

In WPF app I have an Observable Collection which is connected through databinding with ListView. I would like to populate this Observable Collection from LINQ to SQL query, but with no success. In fact my code even doesn't compile (I pointed out the problem lines with comments).

The structure of underlying Observable Collection Class is similar to the structure of SQL table from which I try to get a query.

Please, what's wrong with my approach?

Here is part of my code:

ObservableCollection<ShowsQu> _ShowQuCollection =
            new ObservableCollection<ShowsQu>();

    public ObservableCollection<ShowsQu> ShowQuCollection
    { get { return _ShowQuCollection; } }

    public class ShowsQu
    {
        public string ShowCode { get; set; }
        public DateTime Date { get; set; }
        public TimeSpan Time { get; set; }
        public string Description { get; set; }
    }

    private void VisualizeAllShows()
    {

        MyfirstdbDataContext context = new MyfirstdbDataContext();
        _ShowQuCollection.Clear();

        var sh = from p in context.Shows select p;

        foreach (var p in sh)  
          _ShowCollection.Add(new ShowsQu
            {
                Date = sh.Date, //here is compile-time error
                Time = sh.Time, //here is compile-time error
                Description = sh.Description //here is compile-time error
            });
        }          
    }
+2  A: 

Subst sh / p (sh is the query, p is the current iterator item):

foreach (var p in sh)  {
    _ShowCollection.Add(new ShowsQu {
        Date = p.Date,
        Time = p.Time,
        Description = p.Description
    });
}

BTW, you could just use foreach(var p in context.Shows) {...} here.

Marc Gravell
Yes, it is. Thank a lot! It works. What a stupid mitake I made and wasn't able to localize it for hours!
rem
And also thank you for your last line's tip
rem
Fresh eyes are often useful ;-p
Marc Gravell