【发布时间】:2016-11-11 16:20:24
【问题描述】:
Cast LINQ result to ObservableCollection
Jon Skeet 在这个问题中给出了很好的答案,但我仍然无法自己制作。
对classes 还是新手,所以我仍处于与他们一起学习的阶段。
我已经制作了一个 LINQ to SQL 类,显然有很多自动生成的代码。这是添加类时生成的类的sn-p,与此问题相关。这显然与名为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;
}
我创建了一个工作类,可以将我需要的数据放入 Datagrid, 它从两个日期之间以及具有唯一员工编号的员工中提取数据。这个工作正常,但是直接更新数据到数据库时,界面中的数据没有更新,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 可以理解,但我不知道foo 需要是什么?
var linqResults = foos.Where(f => f.Name == "Widget");
var observable = new ObservableCollection<Foo>(linqResults);
如何创建 Observablecollection 类来保存 LINQ 查询?
这是我试图做的事情,但在查询时给了我一个编译错误。
不能隐式转换类型 'System.Collections.Generic.List' 到 '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();
【问题讨论】: