score:1
Firstly you need to create an IObservable<string>
to abstract the changing the value on your control. The "easiest" way to do this would be with a Subject<string>
, but most likely its the wrong way to do it.
Below is the code you should put into your ViewModel.
IDisposable _searchSubscriber =
_searchString
.Buffer(TimeSpan.FromMillisecond(300))
.Select(searchString =>
Observable.StartAsync(cancelToken =>
Search(searchString, cancelToken)
).Switch()
.ObserveOn(new DispatcherScheduler())
.Subscribe(results => Channels = results);
public Task<List<Channel>> Search(string searchTerm, CancellationToken cancel)
{
var query = dbContext.Channels.Where(x => x.Name.StartsWith(searchTerm));
return query.ToListAsync(cancel);
}
private BehaviorSubject<string> _searchString = new BehaviorSubject<string>("");
public string SearchString
{
get { return _searchString.Value; }
set { _searchString.OnNext(value); OnPropertyChanged("SearchString"); }
}
Rx.net is an extremely powerful library, which of course means it does have a bit of a learning curve (although the fact is this is complex because your problem is complex).
Let me lay it out...
.Buffer(TimeSpan.FromMilliseconds(300))
debounces your query, so it only runs the query once every 300 milliseconds.
Observable.StartAsync(cancelToken => Search(searchString, cancelToken))
creates an Observable for the search task, which will be cancelled when it is disposed.
Select(x => ...).Switch()
takes only the latest query results, and disposes the last query.
ObserveOn(...)
run the following on the scheduler used, make sure you use either DispatchScheduler
if you are using WPF
, or WinformsScheduler
if you use Winforms.
Subscribe(results => ...)
do something with the results.
Source: stackoverflow.com
Related Articles
- run Task as I type with linq(cancel previous Task if still running)
- How to write LINQ query with column name as parameter still in a type safe way
- Query expressions over source type 'dynamic' or with a join sequence of type 'dynamic' are not allowed
- get selected value from combobox with data source is anonymous type
- MVC many to many get entities with of certain type only in code first
- Find a certain value from a source collection in a target collection of different type with linq
- I am having trouble with the code below. It is saying 'results' is an expression type '?'. which is not a collection type
- How to merge a list of lists with same type of items to a single list of items?
- Calling generic method with a type argument known only at execution time
- Cannot initialize type '' with a collection initializer because it does not implement 'System.Collections.IEnumerable'
- What does this C# code with an "arrow" mean and how is it called?
- Linq find all with certain type
- IEnumerable cannot be used with type arguments
- Could not find an implementation of the query pattern for source type 'System.Data.Entity.DbSet'
- Convert CollectionBase to List or data type usable with Linq
- Calculate difference from previous item with LINQ
- Casting to a derived type in a LINQ to Entities query with Table Per Hierarchy inheritance
- Cannot implement type XYZ with a collection initializer because it does not implement 'System.Collections.IEnumerable'
- LINQ select query with Anonymous type and user Defined type
- C# re-use LINQ expression for different properties with same type
- Cannot apply indexing with [] to an expression of type 'method group' SinglePageApp1. Get["/"] Nancy
- This code returns distinct values. However, what I want is to return a strongly typed collection as opposed to an anonymous type
- C# type arguments cannot be inferred from usage in Select with multiple returns
- How to assign a nullable int property in an anonymous type in LINQ with a Union?
- return list with anonymous type in entity framework
- Null Dapper.net query still returning Null Reference Exception with FirstOrDefault()
- How Do I Create an Expression<Func<>> with Type Parameters from a Type Variable
- Return input type of generic with type constraint in LINQ to Entities (EF4.1)
- Linq EF Include() with Select() new type lost included
- LINQ How to define a default type for use with ElementAtOrDefault Operator
- null in Linq query
- Fastest way of retrieving records from Entity Framework DbSet on varchar Unix Timestamp
- Calling sproc from LINQ with an xml parameter
- LINQ to XML filter descendants C#
- Select set of results (filter) using composite key ASP.NET Web API
- group and sum in linq to sql
- C# LINQ DataTable "WHERE NOT IN"
- How to typecast an imported class from an sql table
- Store math operator in variable
- Order By in List<T> with Lambda Expression when foreign key is null
- Find XmlNode where attribute value is contained in string
- Linq - unable to join table due to null value
- Expression Tree Member binding null with coalesce object?
- Querying unique items by date
- Linq simple XML snippet to non-anonymous type
- How to join two LINQ queries
- How to Remove unneccessary list using lambda and functional C# paradigm
- Replace a given value in array with values from second array in C#
- How do you sort a parent and child collection using Linq?
- How do I use LINQ to query for items, but also include missing items?