IQueryable< a> to observableCollection< a>其中a =匿名类型 [英] IQueryable<a> to ObservableCollection<a> where a = anonymous type
问题描述
我想我的listview的datacontext绑定到一个observable集合。现在我有:
I want the datacontext of my listview to be binded to an observable collection. Right now I have:
// CurrentEmploye = some employee
Entities.DatabaseModel m = new Entities.DatabaseModel();
var q = from t in m.TimeSheet
join emp in m.Employees on t.idEmployee equals emp.id
where emp.id == CurrentEmploye.id
select new
{
firstName = emp.firstName,
lastName = emp.lastName,
position = emp.position,
clockInDate = t.clockInDate,
clockOutDate = t.clockOutDate,
};
listView1.DataContext = q;
该代码正确填充listview。现在我想更新listview每当我更新listview项目。
that code populates the listview correctly. Now I will like to update the listview whenever I update a listview item.
我希望变量 q
是类型ObservableCollection,而不必创建一个自定义类,
I will like the variable q
to be of type ObservableCollection without having to create a custom class that holds firstName, lastName, position, etc... How can I do that?
推荐答案
你可以欺骗和创建一个方法来做因为方法可以自动推断泛型类型:
You can cheat and create a method to do it for you since methods can infer the generic type automatically:
public ObservableCollection<T> ToObservableCollection<T>(IEnumerable<T> enumeration)
{
return new ObservableCollection<T>(enumeration);
}
可帮助您将其创建为扩展方法,因此更易于使用...。
Oh, and if it helps you can create this as an extension method so it's easier to use... up to you.
这篇关于IQueryable< a> to observableCollection< a>其中a =匿名类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!