Tono Nam Tono Nam - 4 months ago 32
C# Question

IQueryable<a> to ObservableCollection<a> where a = anonymous type

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
where ==
select new
firstName = emp.firstName,
lastName = emp.lastName,
position = emp.position,
clockInDate = t.clockInDate,
clockOutDate = t.clockOutDate,

listView1.DataContext = q;

that code populates the listview correctly. Now I will like to update the listview whenever I update a listview item.

I will like the variable
to be of type ObservableCollection without having to create a custom class that holds firstName, lastName, position, etc... How can I do that?

m-y m-y

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.