I am new to tasks and I am trying to figure out how to do a query on a list that is returned from aync method usually I would do something like this cause obv linq is not a async method how would one achieve this kinda of secenrio
List<MovieDetail> _mySearchDetail = _myMovie.Where(p => p.name == movietitle).ToList();
But cause its retunning from a task that wont work my two methods are as follows.
This gets the movie details from the webservice
public async Task<List<MovieDetail>> GetMovieDetailsList(int movieId)
{
var tcs = new TaskCompletionSource<List<MovieDetail>>();
string jsonresult = await WCFRESTServiceCall("GET", "movie_details");
var list = await Task.Run(() => jsonresult.Deserialize<MovieDetails>());
tcs.SetResult(list.movieDetaillist);
// for testing to show json being returned
var dialog = new MessageDialog(jsonresult);
await dialog.ShowAsync();
return await tcs.Task;
}
Then what I was wanting to do was build up a search function to allow me to search movie by name , description , actor and what not.
public Task<List<MovieDetail>> searchMovies(string movietitle)
{
List <MovieDetail> _myMovie = await GetMovieDetailsList(1);
var list = await Task.Run(() => how do i query above here ???? >());
List<MovieDetail> _mySearchDetail = _myMovie.Where(p => p.name == movietitle).ToList();
return await tcs.Task;
}
I have included my movieDetail class for completness of code:
public class MovieDetail
{
public string id { get; set; }
public string name { get; set; }
public string description { get; set; }
public string image { get; set; }
public string secondry_images { get; set; }
public string actor { get; set; }
public string actoress { get; set; }
public string director { get; set; }
public string music_director { get; set; }
public string release_date { get; set; }
public string tags { get; set; }
public string age_restriction { get; set; }
public string box_office { get; set; }
public string official_site { get; set; }
public string duration { get; set; }
public string writers { get; set; }
public int imdb { get; set; }
public string status { get; set; }
public string language { get; set; }
public string created { get; set; }
public string modified { get; set; }
public string movie_category_id { get; set; }
public string slug { get; set; }
public string movie_show_time_id { get; set; }
public string theatre_movie_screen_id { get; set; }
public string videous { get; set; }
public string videos { get; set; }
public string comming_soon { get; set; }
public string avg { get; set; }
}
public class MovieDetails
{
public List<MovieDetail> movieDetaillist { get; set; }
}
Why do you use a TaskCompletionSource
in GetMovieDetailsList
?
I'd do it like this:
public async Task<List<MovieDetail>> GetMovieDetailsList(int movieId)
{
string jsonresult = await WCFRESTServiceCall("GET", "movie_details");
var list = jsonresult.Deserialize<MovieDetails>();
return list.movieDetaillist;
}
Your searchMovies
method can look like this:
public async Task<List<MovieDetail>> searchMovies(string movietitle)
{
List <MovieDetail> _myMovie = await GetMovieDetailsList(1);
var list = _myMovie.Where(p => p.name == movietitle).ToList();
return list;
}
await
the time expensive things. It doesn't matter how you got the list. You forget about that the list was built in a task after you get it. You can query it just like you do it with every other list.