setting PageSize when searching

Jul 9, 2009 at 1:51 AM

Hi,

What would be an example of changing the default PageSize when doing a search? I see in Search.cs PageSize has a getter and setter, but in the code sample for SearchTwitterDemo, I can't figure out how to change the default. Would it have to be set in the TwitterContext constructor?

Thanks,
James

Coordinator
Jul 9, 2009 at 2:27 AM

Hi,

You can use the parameters in the where clause filter, like this:

 var queryResults =
                from search in twitterCtx.Search
                where search.Type == SearchType.Search &&
                      search.Query == "LINQ to Twitter" &&
                      search.Page == 2 &&
                      search.PageSize == 5
                select search;

In the example above, the query will return Page 2, where the page size is 5.

Joe

Jul 9, 2009 at 3:12 AM

very cool. Thanks for the quick reply Joe,

James

Sep 9, 2009 at 9:12 PM

Hi,

I'm completely new to LINQ and am struggling to get the search query results from the LINQ expression above to bind to a ListView. I've seen the term "materialize results" used in this forum, is this what I need to do, and how would I do it? Sorry for the newbie question!

Ed

Sep 9, 2009 at 9:30 PM

Hi Ed,

Have you tried the following (with the above code that Joe sent)

var queryResults =
                from search in twitterCtx.Search
                where search.Type == SearchType.Search &&
                      search.Query == "LINQ to Twitter" &&
                      search.Page == 2 &&
                      search.PageSize == 5
                select search;

listView1.DataSource = queryResults;
listView1.DataBind();

James

Sep 10, 2009 at 12:28 PM
Edited Sep 10, 2009 at 12:30 PM

Hi,

I've tried that but it's just binding to the search object not the enumerable entries. I'm using a GridView to test it because you have to define ItemTemplates on a ListView.
The GridView just shows me the properties of the search object like Page and PageSize etc.

I tried casting the queryResults object to an AtomFeed like this to access the Entries collection:

SearchResultsGrid.DataSource = ((AtomFeed)queryResults).Entries;
SearchResultsGrid.DataBind();

But I get this error:

Unable to cast object of type 'LinqToTwitter.TwitterQueryable`1[LinqToTwitter.Search]' to type 'LinqToTwitter.AtomFeed'.

There's clearly some concept (like Materialize results) or something that I'm missing here.

Ed

Sep 10, 2009 at 4:03 PM

I've got it working like this:

List<AtomEntry> r = new List<AtomEntry>();

foreach (var search in queryResults)
{
    r.AddRange(search.Entries);
}

SearchResultsGrid.DataSource = r;
SearchResultsGrid.DataBind();

I know there has to be a better way of doing this purely in LINQ but as I said I'm a total novice.

Sep 10, 2009 at 5:55 PM

Hi Ed,

I haven't used LINQ to Twitter for much more than the simple thing I needed to do - grabbing XML as Atom and parsing it. Looking through the LinqToTwitter demos in the download, there are some examples which may help you out.

Default.aspx.cs in the LinqToTwitterWebFormsDemo, links to a ListView

Program.cs in the LinqToTwitterDemo has some good examples of the various types of criteria you can add to the TwitterContext - UserFriendsQueryDemo, UserFollowersQueryDemo, etc.

Hope this helps,

James