Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ query into ObservableCollection?

Tags:

c#

linq

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();
like image 723
KyloRen Avatar asked Jul 09 '16 02:07

KyloRen


Video Answer


1 Answers

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);
like image 100
har07 Avatar answered Oct 04 '22 08:10

har07