6

Cast LINQ result to ObservableCollection

Jon Skeet give a great answer in the question ,but I still cannot make my own.

Still new to classes, so I am still in the learning phases with them.

I have made a LINQ to SQL class and obviously there is a lot of auto generated code. This is the a snippet of the class generated when adding the class, that is relevant to this question. This is obviously linked to the DataBase Table named Staff_Time_TBL.

public partial class Staff_Time_TBL : INotifyPropertyChanging, INotifyPropertyChanged
    {

        private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

        private long _ID;

        private System.Nullable<System.DateTime> _Date_Data;
    }

I have a working class that I made that gets the data I need into the Datagrid, It pulls data from between two dates and from a Staff Member with a unique staff number. This works fine, but when updating data dirrectly to the database, the data in the interface is not updated ,see this question.

internal class DatabaseQueries
    {
        public static IEnumerable<Staff_Time_TBL> MainTable(DatabaseDataContext database, DateTime fromDate, DateTime toDate, int employeeNumber)
        {
            return database.Staff_Time_TBLs.Where(staff =>
                staff.Date_Data > fromDate &&
                staff.Date_Data < toDate &&
                staff.Staff_No == employeeNumber);
        }

This code in this answer is understandable, but I have no idea what foo would need to be?

var linqResults = foos.Where(f => f.Name == "Widget");

var observable = new ObservableCollection<Foo>(linqResults);

How can I make an Observablecollection Class to hold the LINQ query?

This is what I tried to do something, but gives me a compile error at the query.

Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.ObjectModel.ObservableCollection'

public ObservableCollection<Staff_Time_TBL> observerableInfoData { get; set; }

        public MainWindow()
        {  
            DataContext = this; // required for C# binding
            InitializeComponent();            

                            observerableInfoData = new ObservableCollection<Staff_Time_TBL>();
                observerableInfoData = sql.Staff_Time_TBLs.Where(staff => staff.Staff_No == SelectedEmployee.Key &&
                                               staff.Date_Data == filterFrom &&
                                               staff.Date_Data == filterTo).Select(staff => staff.Info_Data).ToList();
Community
  • 1
  • 1
KyloRen
  • 2,691
  • 5
  • 29
  • 59

2 Answers2

11

Basically, you need to pass IEnumerable<Staff_Time_TBL> result of the actual query to the database to initialize the ObservableCollection<Staff_Time_TBL> :

var linqResults = sql.Staff_Time_TBLs
                     .Where(staff => staff.Staff_No == SelectedEmployee.Key &&
                                     staff.Date_Data == filterFrom &&
                                     staff.Date_Data == filterTo);

var observable = new ObservableCollection<Staff_Time_TBL>(linqResults);
Eli Arbel
  • 22,391
  • 3
  • 45
  • 71
har07
  • 88,338
  • 12
  • 84
  • 137
  • Ahh, I see now. Thanks for that. One question though, how do I make this into a class? I upvoted for the help thus far. – KyloRen Jul 09 '16 at 04:18
  • @KyloRen: As near as I can tell, based on your comment here, this answer has addressed your question and should be accepted. If you have a new question, post a new question, don't tack on new questions this one. – Peter Duniho Jul 09 '16 at 04:20
  • @PeterDuniho, half way through the question I ask this `How can I make an Observablecollection Class to hold the LINQ query?` that is my goal and the purpose of the question. – KyloRen Jul 09 '16 at 04:23
  • @KyloRen: The above answers that question. It shows a LINQ query -- in fact, it shows exactly the LINQ query you provided in your question -- and shows how to create an instance of `ObservableCollection` to hold the results of that LINQ query. Even by your own statement, this is _the_ answer to your question. What part do you not understand? – Peter Duniho Jul 09 '16 at 04:24
  • @PeterDuniho, forgive my ignorance, I am still learning classes. I appreciate the effort by har07 and up voted accordingly, but I still can't get from what he/she posted into a class on its own? – KyloRen Jul 09 '16 at 04:32
  • @KyloRen: "into a class on its own" is not the same question as "make an `ObservableCollection`". The latter question is what you asked. If you want to know about the former, you need to post a new question, and be sure you are very clear about what you mean. I.e. given some specific input, what _exactly_ do you want the outcome to be. – Peter Duniho Jul 09 '16 at 06:10
  • @PeterDuniho, Sorry, did not know that. I will post another question and mark this one solved. – KyloRen Jul 09 '16 at 06:43
  • @PeterDuniho, I just pinging to this question. I can safely confess I see your frustration with my lack of understanding. This seems so easy now that I have 8 months of programming under my belt. Thanks for the patients. – KyloRen Mar 22 '17 at 01:37
0

You need ObservableComputations. Using this library you can code like this:

var linqResults = sql.Staff_Time_TBLs
                     .Filtering(staff =>
                        staff.Date_Data > fromDate &&
                        staff.Date_Data < toDate &&
                        staff.Staff_No == employeeNumber);

In code above I assumed sql.Staff_Time_TBLs is ObservableCollection. linqResults is ObservableCollection and reflects all the changes in the sql.Staff_Time_TBLs collection and properties mentioned in the code. Ensure that all properties mentioned in the code above notify of changes through the INotifyPropertyChanged interface.

Igor Buchelnikov
  • 505
  • 4
  • 14