I have a UI control that accepts Enumerable as a binding source. But until I set the binding source, I have to filter out the original Enumerable . I would like to use LINQ for this purpose:
control.BindingSource = from var item in enumerable.Cast<ItemType>() where item.X == 1 select item;
This is a user interface dependent problem, since an enumerated one is slow (for example, if it is implemented as yield return new Item(); Thread.Sleep(1000) ... ) and tries to control the execution of the request in the user interface thread. I tried to solve this problem using a combination of Task and async-wait:
control.BindingSource = await Task.Factory.StartNew(() => (from var item in enumerable.Cast<ItemType>() where item.X == 1 select item).ToArray());
Now the user interface does not freeze, but the results are displayed immediately after completion of the query. I solve this using ObservableCollection and Enumerator with await next to MoveNext in while :
var source = new ObservableCollection<object>(); control.BindingSource = source; var enumerator = enumerable.GetEnumerator(); while (await Task.Factory.StartNew(() => enumerator.MoveNext())) { var item = (ItemType)enumerator.Current; if (item.X == 1) source.Add(item); }
I am looking for a solution that will allow at least LINQ. Any ideas?
source share