1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
<title>You Tube Linq Example Queries</title>
<style type="text/css">
.csharpcode, .csharpcode pre
{
font-size: 10pt;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<h1>Example Code for performing Linq queries against the YouTubeRequest
object</h1>
<p>by Joe Feser <a href="mailto:joe.feser@joefeser.com">joe.feser@joefeser.com</a></p>
<h2>Introduction</h2>
<p>
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.</p>
<h2>Details</h2>
<p>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.</p>
<h3>Activities Filtering</h3>
<p>
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".
</p>
<p>Example</p>
<!-- code formatted by http://manoli.net/csharpformat/ -->
<pre class="csharpcode">
YouTubeRequestSettings settings = <span class="kwrd">new</span> YouTubeRequestSettings(<span class="str">"NETUnittests"</span>, YTCLIENTID, YTDEVKEY, USERNAME, PASSWORD);
YouTubeRequest f = <span class="kwrd">new</span> YouTubeRequest(settings);
settings.AutoPaging = <span class="kwrd">true</span>;
settings.Maximum = 200;
<span class="rem">//lets try to get the activities</span>
Feed<Activity> activityFeed = f.GetActivities();
<span class="kwrd">var</span> actFiltered = <span class="kwrd">from</span> e <span class="kwrd">in</span> activityFeed.Entries
<span class="kwrd">where</span> e.Title.IndexOf(<span class="str">"Cool"</span>, StringComparison.InvariantCultureIgnoreCase) > -1
<span class="kwrd">where</span> e.Type == ActivityType.Rated
<span class="kwrd">select</span> e;
Console.WriteLine(actFiltered.Count());
</pre>
<h3>Most Popular Feed combined with Comments in an anonymous type</h3>
<p>
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.</p>
<p>Example</p>
<!-- code formatted by http://manoli.net/csharpformat/ -->
<pre class="csharpcode">
YouTubeRequestSettings settings = <span class="kwrd">new</span> YouTubeRequestSettings(<span class="str">"NETUnittests"</span>, YTCLIENTID, YTDEVKEY);
YouTubeRequest f = <span class="kwrd">new</span> YouTubeRequest(settings);
settings.AutoPaging = <span class="kwrd">true</span>;
settings.Maximum = 25;
Feed<Video> sfeed = f.GetStandardFeed(YouTubeQuery.MostPopular);
<span class="kwrd">var</span> results = <span class="kwrd">from</span> e <span class="kwrd">in</span> sfeed.Entries
<span class="kwrd">where</span> e.Updated > <span class="kwrd">new</span> DateTime(2008, 12, 1)
<span class="kwrd">select</span> <span class="kwrd">new</span> {
Entry = e,
Comments = f.GetComments(e)
};
<span class="kwrd">foreach</span> (<span class="kwrd">var</span> item <span class="kwrd">in</span> results) {</pre>
<pre class="csharpcode">
<span class="rem">//Since we created a property called Entry, the Entry from the original query will be accessable as Entry</span>
<span class="kwrd">var</span> author = item.Entry.Author;
<span class="rem">//The comments will be returned only once you access the collection</span>
<span class="rem">//if you never access Comments, the service is never called.</span>
<span class="kwrd">var</span> c = item.Comments.Entries.Count();
}
</pre>
<h3>Example of multiple where clauses and a Lamba expression to sort the results</h3>
<p>
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:
<pre class="csharpcode"><span class="kwrd">var</span> entries = sfeed.Entries.ToList();</pre>
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.
</p>
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.
<pre class="csharpcode"><span class="kwrd">foreach</span> (<span class="kwrd">var</span> item <span class="kwrd">in</span> entries.OrderBy(i => i.Title)) {
Console.WriteLine(item.Title);
}</pre>
You can just as easily performed a double sort, first by the Author and then by the Title Descending. That would look like this:
<pre class="csharpcode"><span class="kwrd">foreach</span> (<span class="kwrd">var</span> item in entries.OrderBy(i => i.Author).ThenByDescending(i => i.Title)) {
Console.WriteLine(item.Title);
}
</pre>
<p>Example</p>
<!-- code formatted by http://manoli.net/csharpformat/ -->
<pre class="csharpcode">
YouTubeRequestSettings settings = <span class="kwrd">new</span> YouTubeRequestSettings(<span class="str">"NETUnittests"</span>, YTCLIENTID, YTDEVKEY);
YouTubeRequest f = <span class="kwrd">new</span> YouTubeRequest(settings);
settings.AutoPaging = <span class="kwrd">true</span>;
settings.Maximum = 200; <span class="rem">//only 75 come back but that is a feature</span>
Feed<Video> sfeed = f.GetStandardFeed(YouTubeQuery.MostPopular);
<span class="rem">//put the entire list into a list.</span>
<span class="kwrd">var</span> entries = sfeed.Entries.ToList();
<span class="kwrd">var</span> oneHunderTitles = <span class="kwrd">from</span> e <span class="kwrd">in</span> entries
<span class="kwrd">where</span> e.ViewCount > 100
<span class="kwrd">where</span> e.Rating > 2
<span class="kwrd">where</span> e.Updated < <span class="kwrd">new</span> DateTime(2008, 12, 4)
<span class="kwrd">orderby</span> e.Rating descending
<span class="kwrd">orderby</span> e.Title
<span class="kwrd">select</span> e;
<span class="kwrd">foreach</span> (<span class="kwrd">var</span> item <span class="kwrd">in</span> oneHunderTitles) {
Console.WriteLine(item.Title);
}
<span class="rem">//here is an inline orderby on title as a lambda</span>
<span class="kwrd">foreach</span> (<span class="kwrd">var</span> item <span class="kwrd">in</span> entries.OrderBy(i => i.Title)) {
Console.WriteLine(item.Title);
}
Console.WriteLine(sfeed.Entries.Count());
</pre>
|