Example Code for performing Linq queries against the YouTubeRequest object

by Joe Feser joe.feser@joefeser.com

Introduction

The Linq samples are not intended to be a Linq provider, they are meant to be used to perform actions against the YouTube responses. You must still initialize the YouTubeRequest just like you would for any other request. The purpose of this document is to show different scenarios for filtering and aggregating the results of your requests.

Details

Your project must be created in vs.net 2008 or later and it must be set up to support the .NET 3.5 runtime. You can verify this setting by right clicking on the project and selecting properties. On the Application tab, the target framework must be v3.5. You must also make sure you have a reference to System.Core and the namespace System.Linq is referenced in the using block at the top of the source file. If you do not perform these steps, you will not receive Intellisense and you will have compiler errors.

Activities Filtering

This example is demonstrating how you can obtain the Activities feed and filter the results where the item has been Rated and the Title contains the string "cool".

Example

YouTubeRequestSettings settings = new YouTubeRequestSettings("NETUnittests", YTCLIENTID, YTDEVKEY, USERNAME, PASSWORD);

YouTubeRequest f = new YouTubeRequest(settings);
settings.AutoPaging = true;
settings.Maximum = 200;

//lets try to get the activities
Feed<Activity> activityFeed = f.GetActivities();

var actFiltered = from e in activityFeed.Entries
                  where e.Title.IndexOf("Cool", StringComparison.InvariantCultureIgnoreCase) > -1
                  where e.Type == ActivityType.Rated
                  select e;

Console.WriteLine(actFiltered.Count());

Most Popular Feed combined with Comments in an anonymous type

This example is demonstrating how we can take the MostPopular list and perform a "join" statement with the comments for the item. The results of this query is an anonymous type that contains the Entry and a reference to the Comments "Request". The cool thing about this query is the comments have not been requested unless you access the Entries property of the object.

Example

YouTubeRequestSettings settings = new YouTubeRequestSettings("NETUnittests", YTCLIENTID, YTDEVKEY);

YouTubeRequest f = new YouTubeRequest(settings);
settings.AutoPaging = true;
settings.Maximum = 25;
Feed<Video> sfeed = f.GetStandardFeed(YouTubeQuery.MostPopular);

var results = from e in sfeed.Entries
              where e.Updated > new DateTime(2008, 12, 1)
              select new {
                  Entry = e,
                  Comments = f.GetComments(e)
              };

foreach (var item in results) {
     //Since we created a property called Entry, the Entry from the original query will be accessable as Entry
    var author = item.Entry.Author;
    //The comments will be returned only once you access the collection
    //if you never access Comments, the service is never called.
    var c = item.Comments.Entries.Count();
}

Example of multiple where clauses and a Lamba expression to sort the results

This example is demonstrating the power of multiple where clauses. They are "pipelined", so in a high performance environment, you would want to put the most likely item to filter first. This works just like short curcuited (&& and ||) if blocks. If the ViewCount is < 0, the Rating will never be checked. We are also showing how you can convert the results to a List<> before enumerating the collection. This is being demonstrated by the following code:

var entries = sfeed.Entries.ToList();
The list is then being enumerated twice, once in the order that the items are returned from the service, and once ordering the list by Title.

The .OrderBy(i => i.Title) portion of the code below is a Selector that is passed into the OrderBy Delegate. The Code reads as follows: declare an variable i and for each item in the collection (entries), return the Title to the delegate, so a Sort can be performed.
foreach (var item in entries.OrderBy(i => i.Title)) {
    Console.WriteLine(item.Title);
}
You can just as easily performed a double sort, first by the Author and then by the Title Descending. That would look like this:
foreach (var item in entries.OrderBy(i => i.Author).ThenByDescending(i => i.Title)) {
    Console.WriteLine(item.Title);
}

Example

YouTubeRequestSettings settings = new YouTubeRequestSettings("NETUnittests", YTCLIENTID, YTDEVKEY);

YouTubeRequest f = new YouTubeRequest(settings);
settings.AutoPaging = true;
settings.Maximum = 200; //only 75 come back but that is a feature
Feed<Video> sfeed = f.GetStandardFeed(YouTubeQuery.MostPopular);

//put the entire list into a list.
var entries = sfeed.Entries.ToList();

var oneHunderTitles = from e in entries
                      where e.ViewCount > 100
                      where e.Rating > 2
                      where e.Updated < new DateTime(2008, 12, 4)
                      orderby e.Rating descending
                      orderby e.Title
                      select e;

foreach (var item in oneHunderTitles) {
    Console.WriteLine(item.Title);
}

//here is an inline orderby on title as a lambda
foreach (var item in entries.OrderBy(i => i.Title)) {
    Console.WriteLine(item.Title);
}

Console.WriteLine(sfeed.Entries.Count());