LINQ查询到ObservableCollection吗? [英] LINQ query into ObservableCollection?
问题描述
将LINQ结果转换为ObservableCollection
乔恩·斯基特(Jon Skeet)在这个问题上给出了很好的答案,但我仍然无法自己做.
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.
我制作了LINQ to SQL类,显然有很多自动生成的代码.这是添加该类时生成的该类的片段,与该问题有关.显然,这已链接到名为Staff_Time_TBL
的数据库表.
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;
}
我有一个工作类,可以将所需的数据输入到Datagrid中, 它从两个日期之间以及具有唯一员工编号的工作人员中提取数据.这可以正常工作,但是当直接将数据更新到数据库时,界面中的数据不会更新,
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);
}
此答案中的这段代码是可以理解的,但是我不知道foo
是什么?
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);
如何制作一个Observablecollection类来容纳LINQ查询?
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.
无法隐式转换类型 'System.Collections.Generic.List'到 'System.Collections.ObjectModel.ObservableCollection'
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();
推荐答案
基本上,您需要将实际查询的IEnumerable<Staff_Time_TBL>
结果传递给数据库以初始化ObservableCollection<Staff_Time_TBL>
:
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);
这篇关于LINQ查询到ObservableCollection吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!