Question

Im a rookie and could use some help.

I was looking at examples of LinqToTwitter and am trying to figure out how to display the results in my view instead of the console.

var srch =
   (from search in twitterCtx.Search
    where search.Type == SearchType.Search &&
    search.Query == "LINQ to Twitter" &&
    search.Count == 7
    select search).SingleOrDefault();

    Console.WriteLine("\nQuery: {0}\n", srch.SearchMetaData.Query);
    srch.Statuses.ForEach(entry =>
        Console.WriteLine(
           "ID: {0, -15}, Source: {1}\nContent: {2}\n",
            entry.StatusID, entry.Source, entry.Text));

I would like to use a ViewModel if possible.

Thank you!

-Update-

Thanks Joe. Here's where I am after your help. I'm stuck at the part where I need to store the results into my viewmodel. I appreciate any help given. Thanks!

Controller

twitterCtx = new TwitterContext(auth);

List<TweetViewModel> TwitterResults = 
(
    from search in twitterCtx.Search
    where search.Type == SearchType.Search &&
    search.ResultType == ResultType.Recent &&
    search.Query == "killsometime" &&
    search.Count == 10
    select new TweetViewModel
    {
        ScreenName = search.Statuses. >> "I'm Stuck Here",
        Text = search.Statuses. >> "I'm Stuck Here"
    }
 ).ToList();

    return View(TwitterResults);

ViewModel

public class TweetViewModel
{
    public string ScreenName { get; set; }
    public string Text { get; set; }
}

View

@model IEnumerable<FVV.ViewModels.TweetViewModel>

@{
    ViewBag.Title = "Index";
}

<ul>
@foreach (var tweet in Model)
{
    <li>ScreenName: @tweet.ScreenName, Tweet: @tweet.Text</li>
}
</ul>
Was it helpful?

Solution

There are ASP.NET MVC samples on the LINQ to Twitter Samples page. The demos might be a little dated, but will probably easy to tweak if there are any problems. You'll probably want to update the NuGet package.

Also, the downloadable source code contains a LinqToTwitterMvcDemo that does a query and loads a collection of TweetViewModel into a view. It isn't part of the solution, but you can right-click on the Solution, select Add, Existing Project, and navigate to the LinqToTwitterMvcDemo folder under the LinqToTwitter folder to find it. Just add your credentials in Web.config and it should work fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top