Searching with params only if they are not null

Aug 2, 2014 at 12:39 PM
Hi,

This may be more of a general Linq question that specifically to do with the LinqToTwitter, but hopefully someone can help.

I'm using the TwitterContext.Search to search for tweets. This is my code and it works well
            var searchResponse =
                           await
                           (from search in twitterCtx.Search
                                where   search.Type == SearchType.Search &&
                                        search.Count == ResultsPerPage &&
                                        search.Query == Query &&
                                        search.ResultType == rt
                                select search)
                           .SingleOrDefaultAsync();
I have some additional parameter such as MaxID, SinceID, Until and GeoCode which may or may not have values.

How do I add these parameter only if they are not null?

For example if MaxID is not null I'd add the following to my query. However if MaxID is null it means that no tweets are returned.
&& search.MaxID == MaxID
Regards
Coordinator
Aug 2, 2014 at 10:27 PM
Hi,

You can continue modifying the query as long as you don't materialize it. Taking your code for example:
        var searchResponse =
                           (from search in twitterCtx.Search
                            where  search.Type == SearchType.Search &&
                                        search.Count == ResultsPerPage &&
                                        search.Query == Query &&
                                        search.ResultType == rt
                            select search)
gives you a basic IQueryable. Notice we aren't awaiting yet. So, you can continue building the query by adding Where clauses, like this
    searchResponse = searchResponse.Where(search => search.MaxID == MaxID)
and finally, after you know you're done:
    var materializedSearchResponse = await searchResponse.SingleOrDefaultAsync();
@JoeMayo
Aug 3, 2014 at 8:04 AM
That is awesome thanks - I thought it would be a more general Linq question. This library rocks, I'm going to use it to build a Twitter app for Office as well as update my 'TileATweet' Windows app.

Here is the final code
            //Search
            var searchResponse = (from search in twitterCtx.Search
                                  where search.Type == SearchType.Search &&
                                          search.Count == ResultsPerPage &&
                                          search.Query == Query &&
                                          search.ResultType == rt
                                  select search);

            if (!string.IsNullOrEmpty(GeoCode)) searchResponse = searchResponse.Where(q => q.GeoCode == GeoCode);
            if (MaxID != 0) searchResponse = searchResponse.Where(q => q.MaxID == MaxID);
            if (SinceID != 0) searchResponse = searchResponse.Where(q => q.SinceID == SinceID);
            if (!string.IsNullOrEmpty(Until)) searchResponse = searchResponse.Where(q => q.Until == Convert.ToDateTime(Until));

            var materializedSearchResponse = await searchResponse.SingleOrDefaultAsync();
Coordinator
Aug 3, 2014 at 6:53 PM