<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>LINQ</title>
        <link>http://geekswithblogs.net/maxonweb/category/11002.aspx</link>
        <description>LINQ</description>
        <language>en-AU</language>
        <copyright>Max</copyright>
        <managingEditor>rm.manickam@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>Silverlight 4 Twitter Client &amp;ndash; Part 5</title>
            <link>http://geekswithblogs.net/maxonweb/archive/2010/03/27/silverlight-4-twitter-client-ndash-part-5.aspx</link>
            <description>&lt;p&gt;In this post, we will look at implementing the following in our twitter client - displaying the profile picture in the home page after logging in. So to accomplish this, we will need to do the following steps.&lt;/p&gt;
&lt;p&gt;1) First of all, let us create another field in our Global variable static class to hold the profile image url. So just create a static string field in that.&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; profileImage { get; set; }&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	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; }]]&gt;&lt;/style&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;2) In the Login.xaml.cs file, before the line where we redirect to the Home page if the login was successful. Create another WebClient request to fetch details from this url - &lt;a href="http://api.twitter.com/1/users/show/&amp;lt;TwitterUsername&amp;gt;.xml"&gt;.xml"&amp;gt;http://api.twitter.com/1/users/show/&amp;lt;TwitterUsername&amp;gt;.xml&lt;/a&gt; I am interested only in the “profile_image_url” node in this xml string returned. You can choose what ever field you need – the reference to the Twitter API Documentation is &lt;a target="_blank" href="http://apiwiki.twitter.com/Twitter-REST-API-Method:-users%C2%A0show"&gt;&lt;strong&gt;&lt;em&gt;here&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;3) This particular url does not require any authentication so no need of network credentials for this and also the useDefaultCredentials will be true. So this code would look like.&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;
WebRequest.RegisterPrefix(&lt;span class="str"&gt;"http://"&lt;/span&gt;, System.Net.Browser.WebRequestCreator.ClientHttp);&lt;/pre&gt;
&lt;pre&gt;
WebClient myService = &lt;span class="kwrd"&gt;new&lt;/span&gt; WebClient();&lt;/pre&gt;
&lt;pre class="alt"&gt;
myService.AllowReadStreamBuffering = myService.UseDefaultCredentials = &lt;span class="kwrd"&gt;true&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;
myService.DownloadStringCompleted += &lt;span class="kwrd"&gt;new&lt;/span&gt; DownloadStringCompletedEventHandler(TimelineRequestCompleted_User);)&lt;/pre&gt;
&lt;pre class="alt"&gt;
myService.DownloadStringAsync(&lt;span class="kwrd"&gt;new&lt;/span&gt; Uri(&lt;span class="str"&gt;"http://api.twitter.com/1/users/show/"&lt;/span&gt; + TwitterUsername.Text + &lt;span class="str"&gt;".xml"&lt;/span&gt;));&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	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; }]]&gt;&lt;/style&gt;
&lt;p&gt;4) To parse the xml file returned by this and get the value of the “profile_image_url” node, the LINQ code will look something like&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;
XDocument xdoc;&lt;/pre&gt;
&lt;pre&gt;
xdoc = XDocument.Parse(e.Result);&lt;/pre&gt;
&lt;pre class="alt"&gt;
var answer = (from status &lt;span class="kwrd"&gt;in&lt;/span&gt; xdoc.Descendants(&lt;span class="str"&gt;"user"&lt;/span&gt;)&lt;/pre&gt;
&lt;pre&gt;
              select status.Element(&lt;span class="str"&gt;"profile_image_url"&lt;/span&gt;).Value);&lt;/pre&gt;
&lt;pre class="alt"&gt;
GlobalVariable.profileImage = answer.First().ToString();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;5) After this, we can then redirect to Home page completing our login formalities.&lt;/p&gt;
&lt;p&gt;6) In the home page, add a image tag and give it a name, something like&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;
&amp;lt;Image Name=&lt;span class="str"&gt;"image1"&lt;/span&gt; Stretch=&lt;span class="str"&gt;"Fill"&lt;/span&gt; Margin=&lt;span class="str"&gt;"29,29,0,0"&lt;/span&gt; Height=&lt;span class="str"&gt;"73"&lt;/span&gt; VerticalAlignment=&lt;span class="str"&gt;"Top"&lt;/span&gt; HorizontalAlignment=&lt;span class="str"&gt;"Left"&lt;/span&gt; Width=&lt;span class="str"&gt;"73"&lt;/span&gt; /&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	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; }]]&gt;&lt;/style&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;7) In the OnNavigated to event in home page, we can set the image source url as below&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;
image1.Source = &lt;span class="kwrd"&gt;new&lt;/span&gt; BitmapImage(&lt;span class="kwrd"&gt;new&lt;/span&gt; Uri(GlobalVariable.profileImage, UriKind.Absolute));&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	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; }]]&gt;&lt;/style&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/maxonweb/WindowsLiveWriter/Silverlight4TwitterClientPart5_13605/image_2.png"&gt;&lt;img title="image" border="0" alt="image" width="304" height="267" style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" src="http://geekswithblogs.net/images/geekswithblogs_net/maxonweb/WindowsLiveWriter/Silverlight4TwitterClientPart5_13605/image_thumb.png" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Hope this helps. If you have any queries / suggestions, please feel free to comment below or contact me.&lt;/p&gt;
&lt;div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:901089d2-f496-4833-b0ff-1c51d662d49d" class="wlWriterEditableSmartContent" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px"&gt;Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/Silverlight"&gt;Silverlight&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/LINQ"&gt;LINQ&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/Twitter+API"&gt;Twitter API&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/Twitter"&gt;Twitter&lt;/a&gt;&lt;/div&gt; &lt;img src="http://geekswithblogs.net/maxonweb/aggbug/138955.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Max</dc:creator>
            <guid>http://geekswithblogs.net/maxonweb/archive/2010/03/27/silverlight-4-twitter-client-ndash-part-5.aspx</guid>
            <pubDate>Sat, 27 Mar 2010 11:03:02 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/maxonweb/comments/138955.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/maxonweb/archive/2010/03/27/silverlight-4-twitter-client-ndash-part-5.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/maxonweb/comments/commentRss/138955.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/maxonweb/services/trackbacks/138955.aspx</trackback:ping>
        </item>
        <item>
            <title>LINQ Part 3 &amp;ndash; LINQ for SQL</title>
            <link>http://geekswithblogs.net/maxonweb/archive/2009/12/22/linq-part-3-ndash-linq-for-sql.aspx</link>
            <description>&lt;p&gt;Now that we have some basics for LINQ for XML. Lets get deep into some using LINQ against Relational databases, aggregate functions, like group by and sort. The database we will be using is books, this can be obtained from &lt;a href="http://www.fehily.com/books/sql3.htm#downloads" target="_blank"&gt;this&lt;/a&gt; page. Once you download the database, mount it in SQL Server by attaching this mdf file. Once the mdf file is mounted, we can start with the following steps to do our first LINQ to SQL console program.&lt;/p&gt;  &lt;p&gt;1) First of all we need to add a new DB by selecting Tools &amp;gt; Connect to Database in VS2008 SP1. And then in the dialog box, we need to specify the connection string parameters.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="New DB Connection" border="0" alt="New DB Connection" src="http://geekswithblogs.net/images/geekswithblogs_net/maxonweb/WindowsLiveWriter/LINQPart2LINQforXML_11F91/New%20DB%20Connection_1.png" width="351" height="485" /&gt; &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;If you are using the DB in your local computer, use &lt;strong&gt;(local) &lt;/strong&gt;for the server name and then give your log on credentials if you’ve setup SQL Authentication. Then you can select the DB you want to connect to in the Connect to DB section. Click OK and its all done.&lt;/p&gt;  &lt;p&gt;2) Then right click on the project in Solution explorer and then add LINQ to SQL class item, name the file as &lt;strong&gt;Books&lt;/strong&gt;. Now you will have a new LINQ to SQL designer window open. Now from the database explorer, drag the tables &lt;strong&gt;“title_author”, “title”, “author”. &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;As the tables are not setup with FK constraints, you would not see the relations between the tables, if the table had some FKeys, you would see the relations between the tables in the designer as a line between tables. Save the changes now.&lt;/p&gt;  &lt;p&gt;3) Once you’ve done with creating your LINQ to SQL classes. You can create an instance of the BooksDataContext like&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;BooksDataContext books = &lt;span class="kwrd"&gt;new&lt;/span&gt; BooksDataContext();&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[


.csharpcode, .csharpcode pre
{
	font-size: small;
	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; }]]&gt;&lt;/style&gt;

&lt;p&gt;Now let us find out all the books in the type of &lt;strong&gt;“children”&lt;/strong&gt;&lt;/p&gt;
&lt;strong&gt;&lt;/strong&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;IEnumerable&amp;lt;title&amp;gt; titles = from p &lt;span class="kwrd"&gt;in&lt;/span&gt; books.titles&lt;/pre&gt;

  &lt;pre&gt;                                        &lt;span class="kwrd"&gt;where&lt;/span&gt; p.type.Equals(&lt;span class="str"&gt;"children"&lt;/span&gt;)&lt;/pre&gt;

  &lt;pre class="alt"&gt;                                        select p;&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[


.csharpcode, .csharpcode pre
{
	font-size: small;
	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; }]]&gt;&lt;/style&gt;

&lt;p&gt;Here we get the list of children rows in a IEnumerable collection, which can be iterated through easily. All the columns in the &lt;strong&gt;titles&lt;/strong&gt; table can be easily accessed as properties for the each of the item in the &lt;strong&gt;titles&lt;/strong&gt; collection.&lt;/p&gt;

&lt;p&gt;Now that we’ve the rows in titles table with type as children, we can then easily iterate through it and get the details.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;foreach&lt;/span&gt; (title t &lt;span class="kwrd"&gt;in&lt;/span&gt; titles)
            {
                Console.WriteLine(&lt;span class="str"&gt;"Type: "&lt;/span&gt; + t.type + &lt;span class="str"&gt;", Title: "&lt;/span&gt; + t.title_name);
            }&lt;/pre&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[


.csharpcode, .csharpcode pre
{
	font-size: small;
	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; }]]&gt;&lt;/style&gt;

&lt;p&gt;Now let us try to get the list of books grouped by the type.&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;var title1 = from p3 &lt;span class="kwrd"&gt;in&lt;/span&gt; books.titles&lt;/pre&gt;

  &lt;pre&gt;                         group p3 by p3.type into g&lt;/pre&gt;

  &lt;pre class="alt"&gt;                         select &lt;span class="kwrd"&gt;new&lt;/span&gt; { Value = g.Key, Total = g };&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[


.csharpcode, .csharpcode pre
{
	font-size: small;
	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; }]]&gt;&lt;/style&gt;

&lt;p&gt;The output will be&lt;/p&gt;

&lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/maxonweb/WindowsLiveWriter/LINQPart2LINQforXML_11F91/image_2.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="image" border="0" alt="image" src="http://geekswithblogs.net/images/geekswithblogs_net/maxonweb/WindowsLiveWriter/LINQPart2LINQforXML_11F91/image_thumb.png" width="590" height="65" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;We group by a particular column value using &lt;strong&gt;group p3 by p3.type into g, &lt;/strong&gt;here g.Key will give you the distinct values in that column by which the values are grouped. g as a whole will have a collection of items for each of the key.&lt;/p&gt;

&lt;p&gt;We can then easily iterate through the list to print the details as given below.&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var g &lt;span class="kwrd"&gt;in&lt;/span&gt; title1)&lt;/pre&gt;

  &lt;pre&gt;            {&lt;/pre&gt;

  &lt;pre class="alt"&gt;                Console.WriteLine(&lt;span class="str"&gt;"Type: "&lt;/span&gt; + g.Value);&lt;/pre&gt;

  &lt;pre&gt;                &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var n &lt;span class="kwrd"&gt;in&lt;/span&gt; g.Total)&lt;/pre&gt;

  &lt;pre class="alt"&gt;                {&lt;/pre&gt;

  &lt;pre&gt;                    Console.WriteLine(&lt;span class="str"&gt;"Title: "&lt;/span&gt; + n.title_name);&lt;/pre&gt;

  &lt;pre class="alt"&gt;                }&lt;/pre&gt;

  &lt;pre&gt;                Console.WriteLine();&lt;/pre&gt;

  &lt;pre class="alt"&gt;            }&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[


.csharpcode, .csharpcode pre
{
	font-size: small;
	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; }]]&gt;&lt;/style&gt;

&lt;p&gt;This would give us a pretty output as below:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/maxonweb/WindowsLiveWriter/LINQPart2LINQforXML_11F91/image_4.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="image" border="0" alt="image" src="http://geekswithblogs.net/images/geekswithblogs_net/maxonweb/WindowsLiveWriter/LINQPart2LINQforXML_11F91/image_thumb_1.png" width="550" height="272" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can get the final code file &lt;a href="http://cid-397644e8d28e79c8.skydrive.live.com/embedicon.aspx/.Public/Program.cs" target="_blank"&gt;here&lt;/a&gt;. Wow! That was cool. Imagine writing SQL Queries via C#!&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:381a0f0c-4675-4f08-949b-2b263dec3170" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/LINQ" rel="tag"&gt;LINQ&lt;/a&gt;&lt;/div&gt; &lt;img src="http://geekswithblogs.net/maxonweb/aggbug/137168.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Max</dc:creator>
            <guid>http://geekswithblogs.net/maxonweb/archive/2009/12/22/linq-part-3-ndash-linq-for-sql.aspx</guid>
            <pubDate>Tue, 22 Dec 2009 12:58:59 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/maxonweb/comments/137168.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/maxonweb/archive/2009/12/22/linq-part-3-ndash-linq-for-sql.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/maxonweb/comments/commentRss/137168.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/maxonweb/services/trackbacks/137168.aspx</trackback:ping>
        </item>
        <item>
            <title>LINQ Part 2 &amp;ndash; LINQ for XML</title>
            <link>http://geekswithblogs.net/maxonweb/archive/2009/12/22/linq-part-2-ndash-linq-for-xml.aspx</link>
            <description>&lt;p&gt;Now that we’ve got some basics of LINQ for XML. I think we should also look into group by queries in LINQ for XML. My motivation here would be to find the total price of books for each of the genre. For this, I should write LINQ something similar to this below:&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;            var query3 = from p2 &lt;span class="kwrd"&gt;in&lt;/span&gt; query_1&lt;/pre&gt;

  &lt;pre&gt;                         group p2 by p2.Element(&lt;span class="str"&gt;"genre"&lt;/span&gt;).Value into b1&lt;/pre&gt;

  &lt;pre class="alt"&gt;                         select &lt;span class="kwrd"&gt;new&lt;/span&gt; { Value = b1.Key, Price = b1 };&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	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; }]]&gt;&lt;/style&gt;

&lt;p&gt;We group by particular node value using &lt;strong&gt;group p2 by p2.Element(&lt;span class="str"&gt;"genre"&lt;/span&gt;).Value into b1, &lt;/strong&gt;here b1.Key will give you the distinct values in that node value by which the values are grouped. b1 as a whole will have a collection of items for each of the key. We can easily iterate through this to find the sum using:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var group &lt;span class="kwrd"&gt;in&lt;/span&gt; query3)&lt;/pre&gt;

  &lt;pre&gt;            {&lt;/pre&gt;

  &lt;pre class="alt"&gt;                Console.WriteLine(&lt;span class="str"&gt;"Genre: "&lt;/span&gt; + group.Value);&lt;/pre&gt;

  &lt;pre&gt;                &lt;span class="kwrd"&gt;decimal&lt;/span&gt; price = 0;&lt;/pre&gt;

  &lt;pre class="alt"&gt;                &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var grp_Price &lt;span class="kwrd"&gt;in&lt;/span&gt; group.Price)&lt;/pre&gt;

  &lt;pre&gt;                {&lt;/pre&gt;

  &lt;pre class="alt"&gt;                    price += Convert.ToDecimal( grp_Price.Element(&lt;span class="str"&gt;"price"&lt;/span&gt;).Value );&lt;/pre&gt;

  &lt;pre&gt;                }&lt;/pre&gt;

  &lt;pre class="alt"&gt;                Console.WriteLine(&lt;span class="str"&gt;"Price: "&lt;/span&gt; + price.ToString()+&lt;span class="str"&gt;"\n"&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre&gt;            }&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[


.csharpcode, .csharpcode pre
{
	font-size: small;
	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; }]]&gt;&lt;/style&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;That is it, now it should give all the genre and total price of books under each genre. The output should be something like&lt;/p&gt;

&lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/maxonweb/WindowsLiveWriter/LINQPart2LINQforXML_14C9F/image_2.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="image" border="0" alt="image" src="http://geekswithblogs.net/images/geekswithblogs_net/maxonweb/WindowsLiveWriter/LINQPart2LINQforXML_14C9F/image_thumb.png" width="612" height="212" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The final code file for this exercise is &lt;a href="http://cid-397644e8d28e79c8.skydrive.live.com/embedicon.aspx/.Public/Program.cs" target="_blank"&gt;here&lt;/a&gt;. Please let me have your feedback via the comments below.&lt;/p&gt;

&lt;p&gt;
  &lt;/p&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:662f96df-30d1-4fb2-ad0e-9476a2f7787f" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/LINQ" rel="tag"&gt;LINQ&lt;/a&gt;&lt;/div&gt; &lt;img src="http://geekswithblogs.net/maxonweb/aggbug/137167.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Max</dc:creator>
            <guid>http://geekswithblogs.net/maxonweb/archive/2009/12/22/linq-part-2-ndash-linq-for-xml.aspx</guid>
            <pubDate>Tue, 22 Dec 2009 12:57:06 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/maxonweb/comments/137167.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/maxonweb/archive/2009/12/22/linq-part-2-ndash-linq-for-xml.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/maxonweb/comments/commentRss/137167.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/maxonweb/services/trackbacks/137167.aspx</trackback:ping>
        </item>
        <item>
            <title>LINQ Part 1 &amp;ndash; LINQ for XML</title>
            <link>http://geekswithblogs.net/maxonweb/archive/2009/12/03/linq-part-1-ndash-linq-for-xml.aspx</link>
            <description>&lt;p&gt;It looked like we should look a bit about LINQ fundamentals before we actually get into the core programming for our Silverlight twitter application. So we’ll look into LINQ for a few more posts, then we will proceed with our usual Silverlight stuff.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;L&lt;/strong&gt;ine &lt;strong&gt;IN&lt;/strong&gt;tegrated &lt;strong&gt;Q&lt;/strong&gt;uery allows us to use Query language to select data from almost any type of data sources like XML, SQL, Objects, etc.... For more information about LINQ, visit &lt;a href="http://msdn.microsoft.com/en-us/netframework/aa904594.aspx" target="_blank"&gt;this&lt;/a&gt; page, I don’t want to get deep into the details.&lt;/p&gt;  &lt;p&gt;You can get the sample XML files I’ve used in this part is &lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:F60BB8FA-6F02-4999-8F5E-9DD4E92C4DA7:fe880eaa-556e-4df4-a0bb-36da9ade9b16" class="wlWriterEditableSmartContent"&gt;&lt;div&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/maxonweb/WindowsLiveWriter/LINQPart1_14571/books_1.xml" target="_blank"&gt;books.xml&lt;/a&gt;&lt;/div&gt;&lt;/div&gt; . Or you get it from &lt;a title="http://msdn.microsoft.com/en-us/library/ms762271(VS.85).aspx" href="http://msdn.microsoft.com/en-us/library/ms762271(VS.85).aspx"&gt;http://msdn.microsoft.com/en-us/library/ms762271(VS.85).aspx&lt;/a&gt;. Go through this xml document, it will help in better understanding.   &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Now let us quickly get into some hands on coding stuff.&lt;/p&gt;  &lt;p&gt;1) Create a new console application C# project and add the book.xml to it. Now your sol. explorer should look something like this&lt;/p&gt;  &lt;p align="center"&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/maxonweb/WindowsLiveWriter/LINQPart1_14571/LINQProject.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="LINQProject" border="0" alt="LINQProject" src="http://geekswithblogs.net/images/geekswithblogs_net/maxonweb/WindowsLiveWriter/LINQPart1_14571/LINQProject_thumb.png" width="189" height="192" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p align="justify"&gt;2) You are now going to load a xml document into a XDocument type variable like this&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;div&gt;     &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;XDocument xmlDoc = XDocument.Load(&lt;span style="color: #006080"&gt;@"C:\Users\Max\Documents\Visual Studio 2008\Projects\XMLLINQ\XMLLINQ\books.xml"&lt;/span&gt;);&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div&gt; &lt;/div&gt;

  &lt;div&gt;Now you have the xml document in xmlDoc variable and now on we’ll be querying against this variable.&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;3) Now I am going to find out all the books with “Computer” as the genre. Which can be queried as below:&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #008000"&gt;//The actual query, where we choose all the books that are of "Computer" genre.&lt;/span&gt;&lt;br /&gt;var query = from p &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; xmlDoc.Elements(&lt;span style="color: #006080"&gt;"catalog"&lt;/span&gt;).Elements(&lt;span style="color: #006080"&gt;"book"&lt;/span&gt;)&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;where&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;)p.Element(&lt;span style="color: #006080"&gt;"genre"&lt;/span&gt;) == &lt;span style="color: #006080"&gt;"Computer"&lt;/span&gt;&lt;br /&gt;            select p;&lt;/pre&gt;
&lt;/div&gt;

&lt;div&gt; &lt;/div&gt;

&lt;div&gt;4) Then the next step is to iterate through this and print out the desired values.&lt;/div&gt;

&lt;div&gt; &lt;/div&gt;

&lt;div&gt;5) Then I am going to use the variable query from step 3 to find out how many Computer genre books are priced more than 40. This can be done using the query below.&lt;/div&gt;

&lt;div&gt; &lt;/div&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;var query2 = from p1 &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; query&lt;br /&gt;             &lt;span style="color: #0000ff"&gt;where&lt;/span&gt; Convert.ToDouble(p1.Element(&lt;span style="color: #006080"&gt;"price"&lt;/span&gt;).Value) &amp;gt; 40.00&lt;br /&gt;             select p1;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;div&gt;5) The final  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:F60BB8FA-6F02-4999-8F5E-9DD4E92C4DA7:f72dca8b-9c13-4aa2-82e0-339f0a64b735" class="wlWriterEditableSmartContent"&gt;&lt;div&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/maxonweb/WindowsLiveWriter/LINQPart1_14571/Program.cs" target="_self"&gt;Program.cs&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;    is here for your reference. The output should be&lt;/div&gt;

&lt;div&gt; &lt;/div&gt;

&lt;div&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/maxonweb/WindowsLiveWriter/LINQPart1_14571/Capture.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Capture" border="0" alt="Capture" src="http://geekswithblogs.net/images/geekswithblogs_net/maxonweb/WindowsLiveWriter/LINQPart1_14571/Capture_thumb.png" width="539" height="171" /&gt;&lt;/a&gt; &lt;/div&gt;

&lt;div&gt; &lt;/div&gt;

&lt;div&gt;We will look into LINQ for SQL, group by and sort queries in next post. Please post leave your comments below.&lt;/div&gt;

&lt;div&gt; &lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:8a2f7dce-54e3-436f-a326-d81e079541bc" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/LINQ" rel="tag"&gt;LINQ&lt;/a&gt;,&lt;a href="http://technorati.com/tags/XML" rel="tag"&gt;XML&lt;/a&gt;&lt;/div&gt; &lt;img src="http://geekswithblogs.net/maxonweb/aggbug/136673.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Max</dc:creator>
            <guid>http://geekswithblogs.net/maxonweb/archive/2009/12/03/linq-part-1-ndash-linq-for-xml.aspx</guid>
            <pubDate>Thu, 03 Dec 2009 09:59:28 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/maxonweb/comments/136673.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/maxonweb/archive/2009/12/03/linq-part-1-ndash-linq-for-xml.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/maxonweb/comments/commentRss/136673.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/maxonweb/services/trackbacks/136673.aspx</trackback:ping>
        </item>
    </channel>
</rss>
