Reputation: 2741
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();
Upvotes: 6
Views: 15107
Reputation: 555
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.
Upvotes: 0
Reputation: 89285
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);
Upvotes: 13