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 Query
- 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
- Create Generic Type with Generic Interface at Run Time
- When selecting an anonymous type with LINQ from EF, is there no way to run a method on an object as you select it?
- Query expressions over source type 'dynamic' or with a join sequence of type 'dynamic' are not allowed
- How To run LINQ queries with Generic type
- get selected value from combobox with data source is anonymous type
- How To run LINQ queries with Type of <T>
- 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()
More Query from same tag
- handling exceptions with linq queries
- Cannot call methods on varbinary using nettopologysuite
- Using Linq for select row from DataTableA where id not in DataTableB
- convert sql query to linq with sum
- Joining & Transposing multiple Lists using LINQ
- How to write a lambda to get one property based on another property in an object
- How can I model this class in a database?
- Entity Framework Linq set property of an object without additional projection by SELECT
- Read cdata and html inside a tag by linqToXml
- SQL to LINQ Left Outer Join with Multiple Conditions
- How to select all objects sharing a property value with a property value in list of objects?
- How to find a value from a specific element by name?
- LINQ Query with method syntax
- linq max date from multiple columns and tables
- Parsing XML and cast elements to a typed collection using LINQ, C#
- How to access different XML nodes which have same parent?
- C# LINQ "inner join" of collections
- LINQ to SQL and Attaching Child Objects
- Select top N records after filtering in each group
- How to solve a SonarQube S3267 warning, "Loops should be simplified with "LINQ" expressions"
- LINQ include nested properties when selecting anonymous type
- Get top result in each parent property in Entity Framework
- Access DB table and for every record in list create a new column in table
- SQL to LINQ with outer apply
- How to get the data of specific user in one row
- Listing words from array with must letters using linq
- LINQ: Group by Date Range within 5 minutes
- MVC 5 Cannot implicitly convert type 'System.Linq.IQueryable to bool?
- how to use linq to sql with if using
- Linq.Expression GetValue in VB?