<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-2230683952377059025</id><updated>2011-07-29T06:01:31.128+03:00</updated><title type='text'>FoxSharp</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>30</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-6826871810842780519</id><published>2009-07-31T05:14:00.001+03:00</published><updated>2009-07-31T05:14:27.719+03:00</updated><title type='text'>LINQ Operators – Part X</title><content type='html'>&lt;p&gt;I have been busy with a series of tasks and couldn't find time to complete operators section. Although I had a safari account via university subscription and could read it online, I ordered a great book &amp;quot;C# 3.0 in a nutshell – O'Reilly – Joseph Albahari &amp;amp; Ben Albahari&amp;quot;. Amazon estimate was I would get it sometime after August 11th but to my surprise I got it yesterday and maybe that has motivated me to spare extra time and write today's (well tonight's) blog. That is not the first book I read about C# and possibly won't be the last but I must admit I liked it very much. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Conversion operators:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;OfType&lt;/strong&gt;: Returns the items from a sequence that are of a given type. This is especially useful with object collections where the collection can have multiple different types and ArrayList is a typical sample.&amp;#160;&amp;#160; &lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;ArrayList al = &lt;span style="color: blue"&gt;new&lt;/span&gt; ArrayList();     &lt;br /&gt;al.Add(5);     &lt;br /&gt;al.Add(7);     &lt;br /&gt;al.Add(&lt;span style="color: red"&gt;&amp;quot;Jim&amp;quot;&lt;/span&gt;);     &lt;br /&gt;al.Add(&lt;span style="color: red"&gt;&amp;quot;Frank&amp;quot;&lt;/span&gt;);     &lt;br /&gt;al.AddRange( Customers.Where( c =&amp;gt; c.Country == &lt;span style="color: red"&gt;&amp;quot;USA&amp;quot;&lt;/span&gt; ).ToList());     &lt;br /&gt;    &lt;br /&gt;al.OfType&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;().Dump(&lt;span style="color: red"&gt;&amp;quot;Integers&amp;quot;&lt;/span&gt;);     &lt;br /&gt;al.OfType&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;().Dump(&lt;span style="color: red"&gt;&amp;quot;Strings&amp;quot;&lt;/span&gt;);     &lt;br /&gt;al.OfType&amp;lt;Customers&amp;gt;().Dump(&lt;span style="color: red"&gt;&amp;quot;Customers&amp;quot;&lt;/span&gt;); &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;and here is the partial output in LinqPad:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_rkddjaBL1xc/SnJTcp9QUjI/AAAAAAAAAEA/d2qb-2017_8/s1600-h/OfType%5B3%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="OfType" border="0" alt="OfType" src="http://lh3.ggpht.com/_rkddjaBL1xc/SnJTdTNbqpI/AAAAAAAAAEE/B8rJ5Lmtpdo/OfType_thumb%5B1%5D.jpg?imgmax=800" width="830" height="398" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;With generic lists it is unlikely that you would need OfType. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Cast&lt;/strong&gt;: Cast returns a sequence attempting to cast each member to a given type. If it can't cast one ten it throws an error.&amp;#160; The difference between OfType and Cast is that, Cast throws an error if it encounters an incompatible type for casting. OfType, on the other hand, ignores incompatible types and simply returns an empty sequence if it cannot cast any member.&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;ArrayList al = &lt;span style="color: blue"&gt;new&lt;/span&gt; ArrayList();     &lt;br /&gt;al.Add(5);     &lt;br /&gt;al.Add(7);     &lt;br /&gt;al.Cast&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;().Dump(&lt;span style="color: red"&gt;&amp;quot;Integers&amp;quot;&lt;/span&gt;);     &lt;br /&gt;al.OfType&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;().Dump(&lt;span style="color: red"&gt;&amp;quot;Strings&amp;quot;&lt;/span&gt;); &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_rkddjaBL1xc/SnJTdnSdV2I/AAAAAAAAAEI/nFFgDZTWz-Q/s1600-h/Cast%5B2%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Cast" border="0" alt="Cast" src="http://lh5.ggpht.com/_rkddjaBL1xc/SnJTeN7K6fI/AAAAAAAAAEM/0Sba0vWA-oY/Cast_thumb.jpg?imgmax=800" width="214" height="143" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;If we try the OfType&amp;lt;string&amp;gt;() with Cast&amp;lt;string&amp;gt;() we will get an exception. Probably, Cast was added to support ArrayList and I can't think of a good usage for it. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;AsEnumerable&lt;/strong&gt;: Upcasts a sequence to an IEnumerable sequence. Converting to an Enumerable effectively makes the sequence local and then you can apply methods that are not supported on a remote (IQueryable) source. For example Linq To SQL doesn't support Last() but you could make the sequence local with AsEnumerable() and apply Last(). This nice sample is borrowed from LinqPad samples:&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Regex wordCounter = &lt;span style="color: blue"&gt;new&lt;/span&gt; Regex (@&lt;span style="color: red"&gt;&amp;quot;\b(\w|[-'])+\b&amp;quot;&lt;/span&gt;);     &lt;br /&gt;    &lt;br /&gt;// Click the 'SQL' tab below after running &lt;span style="color: blue"&gt;this&lt;/span&gt; query -&amp;#160; &lt;br /&gt;// notice that only the topic filtering     &lt;br /&gt;// predicate executes on SQL Server.     &lt;br /&gt;    &lt;br /&gt;var query = MedicalArticles     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Where (article =&amp;gt; article.Topic == &lt;span style="color: red"&gt;&amp;quot;influenza&amp;quot;&lt;/span&gt;)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .AsEnumerable()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Where (article =&amp;gt; wordCounter.Matches (article.Abstract).Count &amp;lt; 100);     &lt;br /&gt;    &lt;br /&gt;query.Dump(); &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The sample is getting medical articles whose topic is &amp;quot;influenza&amp;quot; and abstract is less than 100 words. Getting articles whose topic is &amp;quot;influenza&amp;quot; is trivial and the part before AsEnumerable() does that. That part is converted to this SQL:&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;-- Region Parameters    &lt;br /&gt;&lt;span style="color: blue"&gt;Declare&lt;/span&gt; @p0 VarChar(9) &lt;span style="color: blue"&gt;Set&lt;/span&gt; @p0 = 'influenza'     &lt;br /&gt;-- EndRegion     &lt;br /&gt;&lt;span style="color: blue"&gt;Select&lt;/span&gt; [t0].[ID], [t0].[Topic], [t0].[Abstract]     &lt;br /&gt;&amp;#160; FROM [MedicalArticles] &lt;span style="color: blue"&gt;As&lt;/span&gt; [t0]     &lt;br /&gt;&amp;#160; WHERE [t0].[Topic] = @p0 &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;The tricky part is counting the words. To count words regular expression is used and that wouldn't execute on Linq To SQL. Applying AsEnumerable() converts the sequence to a local one and makes it possible to count the words using regular expression.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;AsQueryable&lt;/strong&gt;: Converts a sequence to an IQueryable either by downcasting or by creating an IQueryable wrapper.&amp;#160; Converted to an IQueryable you can execute the query locally or remotely.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;strong&gt;ToArray&lt;/strong&gt; and &lt;strong&gt;ToList&lt;/strong&gt;: Convert a sequence to an array or generic list respectively. The generated array or list is a snapshot taken at the moment the query is run. That is particularly useful when a query needs to be executed often and a snapshot is sufficient. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;ToDictionary&lt;/strong&gt; and &lt;strong&gt;ToLookup&lt;/strong&gt;: These operators create a snapshot dictionary of a given sequence. A dictionary is a key, element pair and you can access a member using the key as a indexer. ToDictionary creates a dictionary where sequence has&amp;#160; a unique entry per key (typically a primary key as in a table). ToLookup on the hand looks like foreign key and there can be multiple values for the same key in a sequence. The key is used as an indexer in both.&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;var cusDict = Customers.ToDictionary( c =&amp;gt; c.CustomerID, c =&amp;gt; c );    &lt;br /&gt;cusDict[&lt;span style="color: red"&gt;&amp;quot;BONAP&amp;quot;&lt;/span&gt;].Dump();&lt;/div&gt;  &lt;p&gt;gives this result (note how we use the unique CustomerID as a key and the particular value to &amp;quot;BONAP&amp;quot; to access a member in dictionary via indexer):&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_rkddjaBL1xc/SnJTetT4wwI/AAAAAAAAAEQ/FFHbw1bH1gk/s1600-h/ToDictionary%5B2%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ToDictionary" border="0" alt="ToDictionary" src="http://lh3.ggpht.com/_rkddjaBL1xc/SnJTfXOH_2I/AAAAAAAAAEU/i1FMYxyazn0/ToDictionary_thumb.jpg?imgmax=800" width="241" height="244" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;var clookup = Orders.ToLookup( o =&amp;gt; o.CustomerID, o =&amp;gt; o);    &lt;br /&gt;    &lt;br /&gt;clookup[&lt;span style="color: red"&gt;&amp;quot;BONAP&amp;quot;&lt;/span&gt;].Dump(&lt;span style="color: red"&gt;&amp;quot;Orders of customer BONAP&amp;quot;&lt;/span&gt;); &lt;/div&gt;  &lt;p&gt;yields:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_rkddjaBL1xc/SnJTf8f67xI/AAAAAAAAAEY/ZtqZBrq_GrM/s1600-h/ToLookup%5B3%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ToLookup" border="0" alt="ToLookup" src="http://lh6.ggpht.com/_rkddjaBL1xc/SnJTghfRwDI/AAAAAAAAAEc/Wumtn68YazY/ToLookup_thumb%5B1%5D.jpg?imgmax=800" width="829" height="396" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Other operators:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;SequenceEqual:&lt;/strong&gt; Also accepted as a quantifier operator this operator compares two sequences and returns true if the sequences have identical elements and in the same order.&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;var s1 = &lt;span style="color: red"&gt;&amp;quot;ClintEastwood&amp;quot;&lt;/span&gt;.ToLower().OrderBy(l =&amp;gt; l);     &lt;br /&gt;var s2 = &lt;span style="color: red"&gt;&amp;quot;OldWestAction&amp;quot;&lt;/span&gt;.ToLower().OrderBy(l =&amp;gt; l);     &lt;br /&gt;s1.SequenceEqual(s2).Dump(&lt;span style="color: red"&gt;&amp;quot;An anagram &lt;span style="color: blue"&gt;from&lt;/span&gt; wikipedia&amp;quot;&lt;/span&gt;); &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;This concludes the standard Linq operators. There are other extension methods that are not yet available in .Net 3.5 and since it is extensible we should expect more in the future. Happy LINQinq.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-6826871810842780519?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/6826871810842780519/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=6826871810842780519&amp;isPopup=true' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/6826871810842780519'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/6826871810842780519'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/07/linq-operators-part-x.html' title='LINQ Operators – Part X'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/_rkddjaBL1xc/SnJTdTNbqpI/AAAAAAAAAEE/B8rJ5Lmtpdo/s72-c/OfType_thumb%5B1%5D.jpg?imgmax=800' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-5488975144468707748</id><published>2009-07-03T02:53:00.001+03:00</published><updated>2009-07-03T02:53:55.600+03:00</updated><title type='text'>LINQ Operators – Part IX</title><content type='html'>&lt;p&gt;&lt;strong&gt;Element operators:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;First, FirstOrDefault:&lt;/strong&gt; Used to return the first element from a sequence. T-SQL counterpart is &amp;quot;top 1&amp;quot;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Last, LastOrDefault:&lt;/strong&gt; Used to return the last element from a sequence. T-SQL counterpart is &amp;quot;top 1 … order by desc&amp;quot;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Single, SingleOrDefault;&lt;/strong&gt; Used the return the single result from a sequence. Similar to First/FirstOrDefault but this one throws an exception if there are more than 1 results.&lt;/p&gt;  &lt;p&gt;Using these methods you can either get First, Last or Single element from a sequence ( like a &amp;quot;locate&amp;quot; without any additional clause ). Or you can use a boolean predicate to filter &amp;quot;for&amp;quot; a match. Sometimes there wouldn't be a match to the predicate you supplied and no element would be returned from a sequence. That would cause an exception to be thrown unless you have used the FirstOrDefault, LastOrDefault or SingleOrDefault. &amp;quot;Or default&amp;quot; versions return an element from sequence a default( TSource ) – which is &amp;quot;null&amp;quot; for reference types like string, Customer and blank for value types (generally 0).&amp;#160; &lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Customers.Where( c =&amp;gt; c.Country == &lt;span style="color: red"&gt;&amp;quot;UK&amp;quot;&lt;/span&gt;) &lt;/div&gt;  &lt;div&gt;Returns a &lt;em&gt;sequence&lt;/em&gt; of Customers whether it may be 0 or more Customers. We could get the first UK customer as a single &lt;em&gt;element&lt;/em&gt; of Customers type using First like this:&lt;/div&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Customers.Where( c =&amp;gt; c.Country == &lt;span style="color: red"&gt;&amp;quot;UK&amp;quot;&lt;/span&gt;).First()&lt;/div&gt;  &lt;div&gt;or:&lt;/div&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Customers.First( c =&amp;gt; c.Country == &lt;span style="color: red"&gt;&amp;quot;UK&amp;quot;&lt;/span&gt; )&lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;Since the result is now an element we could directly get its properties if we wanted to like:&lt;/div&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Customers.First( c =&amp;gt; c.Country == &lt;span style="color: red"&gt;&amp;quot;UK&amp;quot;&lt;/span&gt; ).CompanyName&lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;The problem with First() is that if the country was something like &amp;quot;Turkey&amp;quot; (there is no customer from Turkey in Northwind sample database) then the sequence that First() would operate on would have no elements to choose from hence an exception would be thrown. FirstOfDefault() on the other hand, would return a default Customers element:&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Customers.First( c =&amp;gt; c.Country == &lt;span style="color: red"&gt;&amp;quot;Turkey&amp;quot;&lt;/span&gt; ) // throws exception     &lt;br /&gt;    &lt;br /&gt;Customers.FirstOrDefault( c =&amp;gt; c.Country == &lt;span style="color: red"&gt;&amp;quot;Turkey&amp;quot;&lt;/span&gt; ) // &lt;span style="color: blue"&gt;default&lt;/span&gt; Customers element - null &lt;/div&gt;  &lt;p&gt;Last and LastOrDefault works like First/FirstOrDefault but is not supported in Linq To SQL. &lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;&lt;span style="color: blue"&gt;int&lt;/span&gt;[] numbers = {3,8,6,8,3,4,7};     &lt;br /&gt;numbers.Last( n =&amp;gt; n % 2 == 0).Dump(); // 4 &lt;/div&gt;  &lt;p&gt;Single and SingleOrDefault is useful when we are getting data using a unique key (typically a primary key):&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;&lt;span style="color: blue"&gt;string&lt;/span&gt; id1 = &lt;span style="color: red"&gt;&amp;quot;BONAP&amp;quot;&lt;/span&gt;;     &lt;br /&gt;&lt;span style="color: blue"&gt;string&lt;/span&gt; id2 = &lt;span style="color: red"&gt;&amp;quot;XXXXX&amp;quot;&lt;/span&gt;;     &lt;br /&gt;    &lt;br /&gt;Customers customer1 =&amp;#160; &lt;br /&gt;&amp;#160; Customers     &lt;br /&gt;&amp;#160; .SingleOrDefault( c =&amp;gt; c.CustomerID == id1);     &lt;br /&gt;    &lt;br /&gt;Customers customer2 =&amp;#160; &lt;br /&gt;&amp;#160; Customers     &lt;br /&gt;&amp;#160; .SingleOrDefault( c =&amp;gt; c.CustomerID == id2);     &lt;br /&gt;    &lt;br /&gt;( customer1 == &lt;span style="color: blue"&gt;null&lt;/span&gt;&amp;#160; &lt;br /&gt;&amp;#160; ? &lt;span style="color: red"&gt;&amp;quot;No such customer exists.&amp;quot;&lt;/span&gt;&amp;#160; &lt;br /&gt;&amp;#160; : customer1.CompanyName ).Dump(&lt;span style="color: red"&gt;&amp;quot;Key:&amp;quot;&lt;/span&gt; + id1);     &lt;br /&gt;&amp;#160;&amp;#160; &lt;br /&gt;( customer2 == &lt;span style="color: blue"&gt;null&lt;/span&gt;&amp;#160; &lt;br /&gt;&amp;#160; ? &lt;span style="color: red"&gt;&amp;quot;No such customer exists.&amp;quot;&lt;/span&gt;&amp;#160; &lt;br /&gt;&amp;#160; : customer2.CompanyName ).Dump(&lt;span style="color: red"&gt;&amp;quot;Key:&amp;quot;&lt;/span&gt; + id2); &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;ElementAt, ElementAtOrDefault:&lt;/strong&gt; Similar to other element operators, returns element at Nth position (or default(T) with ElementAtOrDefault when there is no element at given position). Not supported in Linq To SQL. You can think of it as record number or array position: &lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;IEnumerable&amp;lt;Customers&amp;gt; cus = Customers.AsEnumerable();    &lt;br /&gt;    &lt;br /&gt;cus.ElementAtOrDefault( 34 ).Dump(&lt;span style="color: red"&gt;&amp;quot;34th&amp;quot;&lt;/span&gt;); // HILAA     &lt;br /&gt;cus.ElementAtOrDefault( 134 ).Dump(&lt;span style="color: red"&gt;&amp;quot;134th&amp;quot;&lt;/span&gt;); // null &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;strong&gt;DefaultIfEmpty&lt;/strong&gt;: Is used for returning a default(TSource) when the sequence is empty (normally it would return a &amp;quot;sequence&amp;quot; if existed). Used in &amp;quot;left join&amp;quot; type queries.&amp;#160; &lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Customers.SelectMany (    &lt;br /&gt;&amp;#160;&amp;#160; c =&amp;gt; c.Orders.DefaultIfEmpty (),&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160; (c, o) =&amp;gt; &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CustomerID = c.CustomerID,&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CompanyName = c.CompanyName,&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; oid = ((Int32?)(o.OrderID) == &lt;span style="color: blue"&gt;null&lt;/span&gt;) ? 0 : o.OrderID,&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; OrderDate = o.OrderDate     &lt;br /&gt;&amp;#160;&amp;#160; }     &lt;br /&gt;) &lt;/div&gt;  &lt;p&gt;It supports a parameter of TSource defining &amp;quot;empty&amp;quot; value. Here is a sample from MSDN documentation: &lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;&lt;span style="color: blue"&gt;class&lt;/span&gt; Pet     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt; Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;int&lt;/span&gt; Age { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;static&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;void&lt;/span&gt; DefaultIfEmptyEx2()     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Pet defaultPet = &lt;span style="color: blue"&gt;new&lt;/span&gt; Pet { Name = &lt;span style="color: red"&gt;&amp;quot;Default Pet&amp;quot;&lt;/span&gt;, Age = 0 };     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; List&amp;lt;Pet&amp;gt; pets1 =     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt; List&amp;lt;Pet&amp;gt;{ &lt;span style="color: blue"&gt;new&lt;/span&gt; Pet { Name=&lt;span style="color: red"&gt;&amp;quot;Barley&amp;quot;&lt;/span&gt;, Age=8 },     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt; Pet { Name=&lt;span style="color: red"&gt;&amp;quot;Boots&amp;quot;&lt;/span&gt;, Age=4 },     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt; Pet { Name=&lt;span style="color: red"&gt;&amp;quot;Whiskers&amp;quot;&lt;/span&gt;, Age=1 } };     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;foreach&lt;/span&gt; (Pet pet &lt;span style="color: blue"&gt;in&lt;/span&gt; pets1.DefaultIfEmpty(defaultPet))     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span style="color: red"&gt;&amp;quot;Name: {0}&amp;quot;&lt;/span&gt;, pet.Name);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; List&amp;lt;Pet&amp;gt; pets2 = &lt;span style="color: blue"&gt;new&lt;/span&gt; List&amp;lt;Pet&amp;gt;();     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;foreach&lt;/span&gt; (Pet pet &lt;span style="color: blue"&gt;in&lt;/span&gt; pets2.DefaultIfEmpty(defaultPet))     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span style="color: red"&gt;&amp;quot;\nName: {0}&amp;quot;&lt;/span&gt;, pet.Name);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;/*     &lt;br /&gt;This code produces the following output:     &lt;br /&gt;    &lt;br /&gt;Name: Barley     &lt;br /&gt;Name: Boots     &lt;br /&gt;Name: Whiskers     &lt;br /&gt;    &lt;br /&gt;Name: Default Pet     &lt;br /&gt;*/ &lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-5488975144468707748?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/5488975144468707748/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=5488975144468707748&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/5488975144468707748'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/5488975144468707748'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/07/linq-operators-part-ix.html' title='LINQ Operators – Part IX'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-8483148588530158967</id><published>2009-07-02T04:33:00.001+03:00</published><updated>2009-07-02T04:33:30.104+03:00</updated><title type='text'>LINQ Operators – Part VIII</title><content type='html'>&lt;p&gt;Aggregation operators&lt;/p&gt;  &lt;p&gt;Aggregation operators are Average, Count, LongCount, Sum, Max, Min and Aggregate. Aggregation operators are available only as methods in C# (VB also have a comprehension syntax for aggregation operators).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Average&lt;/strong&gt;: Gets the average of a numeric collection. T-SQL counterpart is Avg()&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Sum&lt;/strong&gt;: Sums the values in a numeric collection. T-SQL counterpart is Sum()&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Min&lt;/strong&gt;,&lt;strong&gt;Max&lt;/strong&gt;: Gets minimum and largest values in a sequence. T-SQL counterparts are Min(), Max()&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Count&lt;/strong&gt;, &lt;strong&gt;LongCount&lt;/strong&gt;: Counts the elements in a collection. T-SQL counterparts are Count(), Count_Big().&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Aggregate&lt;/strong&gt;: Performs custom aggregation on a set of values. T-SQL has no counterpart.&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;&lt;span style="color: blue"&gt;int&lt;/span&gt;[] scores = {60,75,89};     &lt;br /&gt;scores.Average().Dump(&lt;span style="color: red"&gt;&amp;quot;Average&amp;quot;&lt;/span&gt;);     &lt;br /&gt;scores.Sum().Dump(&lt;span style="color: red"&gt;&amp;quot;Sum&amp;quot;&lt;/span&gt;);     &lt;br /&gt;scores.Max().Dump(&lt;span style="color: red"&gt;&amp;quot;Max&amp;quot;&lt;/span&gt;);     &lt;br /&gt;scores.Min().Dump(&lt;span style="color: red"&gt;&amp;quot;Min&amp;quot;&lt;/span&gt;);     &lt;br /&gt;scores.Count().Dump(&lt;span style="color: red"&gt;&amp;quot;Count&amp;quot;&lt;/span&gt;);     &lt;br /&gt;scores.LongCount().Dump(&lt;span style="color: red"&gt;&amp;quot;LongCount&amp;quot;&lt;/span&gt;); &lt;/div&gt;  &lt;p&gt;Count and LongCount have a filtering parameter that filters what to count based on a boolean expression as in this sample:&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;&lt;span style="color: blue"&gt;int&lt;/span&gt;[] numbers = {17,122,23,12,22};     &lt;br /&gt;numbers.Count( n =&amp;gt; n%2 == 0).Dump(&lt;span style="color: red"&gt;&amp;quot;Even Numbers count&amp;quot;&lt;/span&gt;); &lt;/div&gt;  &lt;p&gt;Count and LongCount are same except that LongCount returns a long (64 bits integer).&lt;/p&gt;  &lt;p&gt;All these methods (except Count and LongCount) have a &amp;quot;selector&amp;quot; parameter to specify &amp;quot;what&amp;quot; to sum,average,min or max. Here are some samples:&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;   &lt;p&gt;Customers.Select(&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160; c =&amp;gt; &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; customer = c.CompanyName,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; firstPurchase = c.Orders.Min( o =&amp;gt; o.OrderDate ),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; lastPurchase = c.Orders.Max( o =&amp;gt; o.OrderDate ),&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; orders = c.Orders.Count,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; totalSale = c.Orders.Count == 0 ? 0 :       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c.Orders.Sum( o =&amp;gt; o.OrderDetails       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Sum( od =&amp;gt; od.Quantity * od.UnitPrice ))       &lt;br /&gt;&amp;#160;&amp;#160; } )       &lt;br /&gt;&amp;#160;&amp;#160; .OrderBy( c =&amp;gt; c.customer )&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;This sample uses Min, Max, Count, Sum to generate a summary statistics on customer purchases. Here is a sample for getting averages:&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;&lt;span style="color: blue"&gt;double&lt;/span&gt; quantity = OrderDetails.Average( od =&amp;gt; od.Quantity );     &lt;br /&gt;&lt;span style="color: blue"&gt;decimal&lt;/span&gt; price =&amp;#160; OrderDetails.Average( od =&amp;gt;&amp;#160; od.UnitPrice );     &lt;br /&gt;&lt;span style="color: blue"&gt;decimal&lt;/span&gt; sale = OrderDetails.Average( od =&amp;gt; od.Quantity * od.UnitPrice );     &lt;br /&gt;    &lt;br /&gt;Console.WriteLine(&lt;span style="color: red"&gt;&amp;quot;{0} {1} {2}&amp;quot;&lt;/span&gt;, quantity, price, sale ); &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Aggregate is the interesting one here that is new to VFP developers (and SQL developers as well). Aggregate allows custom aggregations to be done and is supported only in local data (not supported by Linq To SQL). Here are some samples: &lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;IEnumerable&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; numbers = Enumerable.Range(1,10);     &lt;br /&gt;&lt;span style="color: blue"&gt;int&lt;/span&gt; sum = numbers.Aggregate(&amp;#160; &lt;br /&gt;&amp;#160; (current, next) =&amp;gt; current + next );     &lt;br /&gt;&lt;span style="color: blue"&gt;int&lt;/span&gt; sumProduct = numbers.Aggregate(&amp;#160; &lt;br /&gt;&amp;#160; (current, next) =&amp;gt; current * next );     &lt;br /&gt;    &lt;br /&gt;sum.Dump();     &lt;br /&gt;sumProduct.Dump();     &lt;br /&gt;&lt;/div&gt;  &lt;p&gt;First one is simply a custom implementation of Sum(). Second one gets a product of the numbers in list ( 1 * 2 * 3 * 4 … 10 ). Next example shows that to use aggregate it doesn't have to be a numeric. &lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Customers.Select(c =&amp;gt; c.CustomerID).ToArray().    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Aggregate(&amp;#160;&amp;#160; (current, next) =&amp;gt;&amp;#160; current&amp;#160; + &lt;span style="color: red"&gt;&amp;quot;, &amp;quot;&lt;/span&gt; +next ) &lt;/div&gt;  &lt;p&gt;This sample creates a comma separated list of all customer ID values. Note that we get the customer IDs to an array first to make the collection local.&lt;/p&gt;  &lt;p&gt;Aggregate have two more signatures. Next one uses an initial &amp;quot;seed&amp;quot; value (in the above samples the seed value was the first element in the list). I think VS Linq samples have one of the best samples for this one:&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [Category(&lt;span style="color: red"&gt;&amp;quot;Aggregate Operators&amp;quot;&lt;/span&gt;)]     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [Title(&lt;span style="color: red"&gt;&amp;quot;Aggregate - Seed&amp;quot;&lt;/span&gt;)]     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [Description(&lt;span style="color: red"&gt;&amp;quot;This sample uses Aggregate to create a running account balance that &amp;quot;&lt;/span&gt; +     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: red"&gt;&amp;quot;subtracts each withdrawal &lt;span style="color: blue"&gt;from&lt;/span&gt; the initial balance of 100, &lt;span style="color: blue"&gt;as&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;long&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;as&lt;/span&gt; &amp;quot;&lt;/span&gt; +     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: red"&gt;&amp;quot;the balance never drops below 0.&amp;quot;&lt;/span&gt;)]     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;void&lt;/span&gt; Linq93() {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;double&lt;/span&gt; startBalance = 100.0;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;int&lt;/span&gt;[] attemptedWithdrawals = { 20, 10, 40, 50, 10, 70, 30 };     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;double&lt;/span&gt; endBalance =&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; attemptedWithdrawals.Aggregate(startBalance,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (balance, nextWithdrawal) =&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ( (nextWithdrawal &amp;lt;= balance) ? (balance - nextWithdrawal) : balance ) );     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span style="color: red"&gt;&amp;quot;Ending balance: {0}&amp;quot;&lt;/span&gt;, endBalance);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Next version of Aggregate allows you to have a custom selector projection on the result.&amp;#160; &lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;   &lt;p&gt;Products.Select( p =&amp;gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; { p.ProductName, p.Category.CategoryName }).ToArray()       &lt;br /&gt;.Aggregate(&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt; {Beverages=0,Condiments=0,Other=0}, // initial seed value       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (acc,next) =&amp;gt; next.CategoryName == &lt;span style="color: red"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ? &lt;span style="color: blue"&gt;new&lt;/span&gt; {Beverages=acc.Beverages+1, Condiments=acc.Condiments, Other=acc.Other}       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; : ( next.CategoryName == &lt;span style="color: red"&gt;&amp;quot;Condiments&amp;quot;&lt;/span&gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ? &lt;span style="color: blue"&gt;new&lt;/span&gt; {Beverages=acc.Beverages, Condiments=acc.Condiments+1, Other=acc.Other}       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; : &lt;span style="color: blue"&gt;new&lt;/span&gt; {Beverages=acc.Beverages, Condiments=acc.Condiments, Other=acc.Other+1} ),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; result =&amp;gt; String.Format(&lt;span style="color: red"&gt;&amp;quot;Beverages: {0}, Condiments: {1}, Others:{2}&amp;quot;&lt;/span&gt;, result.Beverages, result.Condiments, result.Other)&amp;#160; // projection       &lt;br /&gt;)&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;It looks confusing and yes it is. Probably you wouldn't want to aggregate like this but use a for loop. I wrote this one just to show how you could initialize a complex (here an anonymous type with Beverages,Condiments and Other properties) seed value, do accumulation based on category names and finally project the result to a custom string.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-8483148588530158967?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/8483148588530158967/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=8483148588530158967&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/8483148588530158967'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/8483148588530158967'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/07/linq-operators-part-viii.html' title='LINQ Operators – Part VIII'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-4260274603447823352</id><published>2009-06-25T02:31:00.001+03:00</published><updated>2009-06-25T02:31:57.985+03:00</updated><title type='text'>LINQ Operators – Part VII</title><content type='html'>&lt;p&gt;&lt;strong&gt;Generation operators:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Range&lt;/strong&gt;: Given a start and count generates a list of integers.&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Enumerable.Range( 3, 10) &lt;/div&gt;  &lt;p&gt;Generates integers 3,4 … 12.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Repeat&lt;/strong&gt;: Is similar to replicate() –but not limited to strings- and generates a list of a given element (either a single element or not) repeating it N times.&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;   &lt;p&gt;Enumerable( 'A', 5).Dump(); // Char A 5 times      &lt;br /&gt;Enumrable.Repeat(1, 3).Dump(); // &lt;span style="color: blue"&gt;int&lt;/span&gt; 1 3 times       &lt;br /&gt;var o = Customers.Single( c =&amp;gt; c.CustomerID == &lt;span style="color: red"&gt;&amp;quot;ALFKI&amp;quot;&lt;/span&gt; ); // Single is a method specifying that the result is a single element       &lt;br /&gt;Enumerable.Repeat(o,10).Dump(); Customers o 10 Times &lt;/p&gt;    &lt;p&gt;var customers = Customers.Where( c =&amp;gt; c.Country == &lt;span style="color: red"&gt;&amp;quot;USA&amp;quot;&lt;/span&gt;&amp;#160; );       &lt;br /&gt;Enumerable.Repeat(customers, 3).Dump(); // a series of customers repeated 3 times&amp;#160; &lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Note that Repeat() repeats the given source which is a singleton element or a collection. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Empty&lt;/strong&gt;: Generates an &amp;quot;empty&amp;quot; sequence of a given type of source. Though it is &amp;quot;empty&amp;quot;, it is still a representation of given type rather than NULL.&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;var customers = Enumerable.Empty&amp;lt;Customers&amp;gt;();    &lt;br /&gt;customers.Dump(); // nothing     &lt;br /&gt;customers.Count().Dump(); // 0 &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Quantifier Operators:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Contains&lt;/strong&gt;: Returns true if a given sequence contains a given element. T-SQL counterpart is IN, Exists.&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;&lt;span style="color: blue"&gt;string&lt;/span&gt;[] countries = &lt;span style="color: red"&gt;&amp;quot;USA,UK,Germany&amp;quot;&lt;/span&gt;.Split(',');     &lt;br /&gt;Customers.Where( c =&amp;gt; countries.Contains(c.Country)).Dump();&lt;/div&gt;  &lt;p&gt;Selects customers where Customers.Country IN (&amp;quot;USA&amp;quot;,&amp;quot;UK&amp;quot;,&amp;quot;Germany&amp;quot;).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Any&lt;/strong&gt;: Returns true if a sequence have &amp;quot;any&amp;quot; elements.&amp;#160; T-SQL counterpart is Exists, IN.&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Customers.Where( c =&amp;gt; c.Orders.Any() ) &lt;/div&gt;  &lt;p&gt;Returns all Customers that has at least one Order. &lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Customers.Where(c =&amp;gt; c.Orders.Any( o =&amp;gt; o.OrderDetails.Count() &amp;gt; 5)) &lt;/div&gt;  &lt;p&gt;Returns all customers who have an Order with at least 6 items. Here is the result set using Northwind (picture cut):&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_rkddjaBL1xc/SkK3a0qsNeI/AAAAAAAAAD4/PwInpb2ToyY/s1600-h/Any%5B3%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Any" border="0" alt="Any" src="http://lh4.ggpht.com/_rkddjaBL1xc/SkK3bR8sYgI/AAAAAAAAAD8/DzPqNKULNNo/Any_thumb%5B1%5D.jpg?imgmax=800" width="737" height="168" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;As you may have noticed Any() can do what Contains() do. For example we could rewrite Contains sample:&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;&lt;span style="color: blue"&gt;string&lt;/span&gt;[] countries = &lt;span style="color: red"&gt;&amp;quot;USA,UK,Germany&amp;quot;&lt;/span&gt;.Split(',');     &lt;br /&gt;Customers     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; .AsEnumerable()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; .Where( c =&amp;gt; countries.Any( cn =&amp;gt; cn == c.Country )).Dump();&lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;However in case of an IQueryable (like Linq To SQL or Entity Framework) Contains() is supported to have a local sequence while Any() is not supported - and its syntax is easier for this sample. &lt;/p&gt;  &lt;p&gt;All: All is like Any() but always have a filter condition where all elements of a sequence should match for All() to return true.&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Orders    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; .Where( o =&amp;gt; o.OrderDetails     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .All( od =&amp;gt; od.Product.Category.CategoryName == &lt;span style="color: red"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt; )) &lt;/div&gt;  &lt;p&gt;Selects all the orders where all order items are from &amp;quot;Beverages&amp;quot; category. You can take a look what those order ID, product name and category names are using the methods we learned earlier:&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Orders    &lt;br /&gt;&amp;#160; .Where( o =&amp;gt; o.OrderDetails     &lt;br /&gt;&amp;#160; .All( od =&amp;gt; od.Product.Category.CategoryName == &lt;span style="color: red"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt; ))     &lt;br /&gt;&amp;#160; .SelectMany(&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; o =&amp;gt; o.OrderDetails,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; (o, od) =&amp;gt; &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; OrderID = o.OrderID,&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Product = od.Product.ProductName,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Category = od.Product.Category.CategoryName     &lt;br /&gt;&amp;#160;&amp;#160; })     &lt;br /&gt;&amp;#160; .OrderBy( o =&amp;gt; o.OrderID )&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-4260274603447823352?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/4260274603447823352/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=4260274603447823352&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/4260274603447823352'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/4260274603447823352'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/06/linq-operators-part-vii.html' title='LINQ Operators – Part VII'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/_rkddjaBL1xc/SkK3bR8sYgI/AAAAAAAAAD8/DzPqNKULNNo/s72-c/Any_thumb%5B1%5D.jpg?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-1149745556501787599</id><published>2009-06-22T01:05:00.001+03:00</published><updated>2009-06-22T01:05:50.987+03:00</updated><title type='text'>LINQ Query Operators – Part VI</title><content type='html'>&lt;p&gt;&lt;strong&gt;Set operators&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;In set operators we will discuss Distinct(), Union(), Intersect(), Except() and Concat().&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Distinct&lt;/strong&gt;: Removes the duplications and selects unique items. T-SQL counterpart is &amp;quot;Distinct&amp;quot;. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Union:&lt;/strong&gt; Returns the union of items from multiple sets. In other words adds one set's elements to other removing duplicates. T-SQL counterpart is &amp;quot;Union&amp;quot;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Concat:&lt;/strong&gt; Returns the union of two sets without dropping the duplicates. T-SQL counterpart is &amp;quot;Union All&amp;quot;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Intersect&lt;/strong&gt;: Returns elements from two sets which exists in both sets. T-SQL counterpart &amp;quot;exists&amp;quot;, &amp;quot;In (….)&amp;quot; (SQL2008 &amp;quot;intersect&amp;quot;).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Except&lt;/strong&gt;: Returns elements from a set which doesn't exist in other set. T-SQL counterpart &amp;quot;not exists&amp;quot;, &amp;quot;not In (….)&amp;quot; (SQL2008 &amp;quot;except&amp;quot;).&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Customers.Select(c=&amp;gt;c.Country).Distinct() &lt;/div&gt;  &lt;p&gt;is the same as:&amp;#160;&amp;#160; &lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;&lt;span style="color: blue"&gt;Select&lt;/span&gt; DISTINCT Country FROM Customers&amp;#160; &lt;/div&gt;  &lt;p&gt;If we have used:&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Customers.Distinct() &lt;/div&gt;  &lt;p&gt;The SQL would be:&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;&lt;span style="color: blue"&gt;Select&lt;/span&gt; * from Customers &lt;/div&gt;  &lt;p&gt;Just as we expected. (The actual generated SQL lists the fields instead of using *)&lt;/p&gt;  &lt;p&gt;Distinct accepts a parameter to define an &amp;quot;equality&amp;quot; interface (IEqualityComparer&amp;lt;TSource&amp;gt;). So you can for example accept the items that have say &amp;quot;Netherlands&amp;quot; and &amp;quot;Holland&amp;quot; as same while applying a distinct operation. This IEqualityComparer interface is not only used by Distinct but by a series of methods like Group, GroupJoin, Intersect, Except, Union. Before we go into sampling IEqualityComparer lets make a note that objects are compared using equality comparison and hashcode. Hashcode is a &amp;quot;signature&amp;quot; of object. Two different objects have different hashcode even if their properties are same. It is not hard to understand this. In VFP consider:&lt;/p&gt;  &lt;p&gt;use myTable    &lt;br /&gt;scatter name o1     &lt;br /&gt;skip     &lt;br /&gt;scatter name o2&lt;/p&gt;  &lt;p&gt;First two records might exactly have the same data in them and hence o1, o2 would match in their properties but still they are 2 different objects as shown in the sample code below:&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Create Cursor sampledata ( firstName c(10), lastname c(10))    &lt;br /&gt;Insert Into sampledata Values (&lt;span style="color: red"&gt;&amp;quot;Joe&amp;quot;&lt;/span&gt;,&lt;span style="color: red"&gt;&amp;quot;Doe&amp;quot;&lt;/span&gt;)     &lt;br /&gt;Insert Into sampledata Values (&lt;span style="color: red"&gt;&amp;quot;Joe&amp;quot;&lt;/span&gt;,&lt;span style="color: red"&gt;&amp;quot;Doe&amp;quot;&lt;/span&gt;)     &lt;br /&gt;Insert Into sampledata Values (&lt;span style="color: red"&gt;&amp;quot;Frank&amp;quot;&lt;/span&gt;,&lt;span style="color: red"&gt;&amp;quot;Doe&amp;quot;&lt;/span&gt;)     &lt;br /&gt;    &lt;br /&gt;Locate     &lt;br /&gt;Scatter Name o1     &lt;br /&gt;Skip     &lt;br /&gt;Scatter Name o2     &lt;br /&gt;    &lt;br /&gt;Clear     &lt;br /&gt;? o1.firstName,o1.lastname     &lt;br /&gt;? o2.firstName,o2.lastname     &lt;br /&gt;    &lt;br /&gt;? &lt;span style="color: red"&gt;&amp;quot;FirstNames are equal&amp;quot;&lt;/span&gt;,o1.firstName == o2.firstName     &lt;br /&gt;? &lt;span style="color: red"&gt;&amp;quot;LastNames are equal&amp;quot;&lt;/span&gt;, o1.lastname == o2.lastname     &lt;br /&gt;? &lt;span style="color: red"&gt;&amp;quot;Objects have identical properties&amp;quot;&lt;/span&gt;, Compobj(o1,o2)     &lt;br /&gt;? &lt;span style="color: red"&gt;&amp;quot;Objects are same&amp;quot;&lt;/span&gt;, o1 = o2     &lt;br /&gt;&lt;/div&gt;  &lt;p&gt;Lets create some SQL server data to see all these visually:&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;create database LinqTest    &lt;br /&gt;go     &lt;br /&gt;    &lt;br /&gt;use LinqTest     &lt;br /&gt;go     &lt;br /&gt;    &lt;br /&gt;create table badData (     &lt;br /&gt;&amp;#160; personId int identity primary key,     &lt;br /&gt;&amp;#160; firstName varchar(15),&amp;#160; lastName varchar(20),     &lt;br /&gt;&amp;#160; email1 varchar(50),&amp;#160; email2 varchar(50),     &lt;br /&gt;&amp;#160; phone1 varchar(15),&amp;#160; phone2 varchar(15),&amp;#160; phone3 varchar(15))     &lt;br /&gt;    &lt;br /&gt;insert into badData&amp;#160; &lt;br /&gt;&amp;#160; (firstName,lastName, email1,email2, phone1,phone2,phone3)     &lt;br /&gt;&amp;#160; values     &lt;br /&gt;&amp;#160; ('John', 'Doe', 'jdoe@myhost1.com','jdoe@myhost2.com','111-111 11 11', '111-111 11 12', '111-111 11 13')     &lt;br /&gt;    &lt;br /&gt;insert into badData&amp;#160; &lt;br /&gt;&amp;#160; (firstName,lastName, email1,email2,phone1,phone2,phone3)     &lt;br /&gt;&amp;#160; values     &lt;br /&gt;&amp;#160; ('John', 'Doe', 'jdoe@myhost1.com','jdoe@myhost2.com','111-111 11 11', '111-111 11 12', '111-111 11 13')     &lt;br /&gt;    &lt;br /&gt;insert into badData&amp;#160; &lt;br /&gt;&amp;#160; (firstName,lastName, email1,email2,phone1,phone2,phone3)     &lt;br /&gt;&amp;#160; values     &lt;br /&gt;&amp;#160; ('John', 'Doe', 'jdoe@myhost2.com','jdoe@myhost1.com','111-111 11 13', '111-111 11 11', '111-111 11 12')     &lt;br /&gt;    &lt;br /&gt;insert into badData&amp;#160; &lt;br /&gt;&amp;#160; (firstName,lastName, email1,email2,phone1,phone2,phone3)     &lt;br /&gt;&amp;#160; values     &lt;br /&gt;&amp;#160; ('Joe', 'Doe','jdoe@myhost2.com','',&amp;#160; '', '', '')     &lt;br /&gt;&amp;#160; &lt;br /&gt;insert into badData&amp;#160; &lt;br /&gt;&amp;#160; (firstName,lastName, email1,email2,phone1,phone2,phone3)     &lt;br /&gt;&amp;#160; values     &lt;br /&gt;&amp;#160; ('Joseph', 'Doe', '','', '111-111 11 13', '', '')     &lt;br /&gt;    &lt;br /&gt;insert into badData&amp;#160; &lt;br /&gt;&amp;#160; (firstName,lastName, email1,email2,phone1,phone2,phone3)     &lt;br /&gt;&amp;#160; values     &lt;br /&gt;&amp;#160; ('Frank', 'Doe', 'fdoe@myhost1.com','fdoe@myhost2.com','111-111 11 11', '', '')     &lt;br /&gt;    &lt;br /&gt;insert into badData&amp;#160; &lt;br /&gt;&amp;#160; (firstName,lastName, email1,email2,phone1,phone2,phone3)     &lt;br /&gt;&amp;#160; values     &lt;br /&gt;&amp;#160; ('John', 'Doe','jdoe@myhost3.com','jdoe@myhost4.com', '222-111 11 11', '222-111 11 12', '222-111 11 13')     &lt;br /&gt;go     &lt;br /&gt;    &lt;br /&gt;&lt;span style="color: blue"&gt;Select&lt;/span&gt; * from badData &lt;/div&gt;  &lt;p&gt;Create the same data as badData2 – to use with Union and see the effect of hashcode. Now we will call Distinct() and Union() using this data. Before doing that lets check our data. It is a bad data design, but here did that on purpose. First two are plain duplicate entry. 3rd one is a duplication where data is in different columns (email1, email2 swapped, phone1,phone2,phone3 are scrambled). 4th, 5th and 6th have different names and only part of the data is entered but they match to others, which is likely to be another kind of duplication (or not a duplication but suppose we want to call offices and call each office only once, then first 6 are the same office). Last one is a match on name but neither his emails nor phones match others which we would think of as another person. Here is our code:&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;&lt;span style="color: blue"&gt;void&lt;/span&gt; Main()     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160; var b1 =     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; BadData.Select( b =&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt; Person {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FirstName = b.FirstName, LastName = b.LastName,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Email1 = b.Email1, Email2 = b.Email2,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Phone1 = b.Phone1, Phone2 = b.Phone2, Phone3 = b.Phone3     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; });     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160; var b2 =     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; BadData2.Select( b =&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt; Person {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FirstName = b.FirstName, LastName = b.LastName,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Email1 = b.Email1, Email2 = b.Email2,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Phone1 = b.Phone1, Phone2 = b.Phone2, Phone3 = b.Phone3     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; });     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160; b1.Union( b2 ).Dump(&lt;span style="color: red"&gt;&amp;quot;b1 union b2 - Union &lt;span style="color: blue"&gt;in&lt;/span&gt; SQL&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160; b1.Distinct().Dump(&lt;span style="color: red"&gt;&amp;quot;b1 distinct - Distinct &lt;span style="color: blue"&gt;in&lt;/span&gt; SQL&amp;quot;&lt;/span&gt;);     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160; PersonEqualityComparer cmp = &lt;span style="color: blue"&gt;new&lt;/span&gt; PersonEqualityComparer();     &lt;br /&gt;&amp;#160;&amp;#160; b1.AsEnumerable().Union( b2.AsEnumerable(), cmp ).Dump(&lt;span style="color: red"&gt;&amp;quot;b1 union b2 with comparer&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160; b1.AsEnumerable().Distinct( cmp ).Dump(&lt;span style="color: red"&gt;&amp;quot;b1 distinct with comparer&amp;quot;&lt;/span&gt;);     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;// Define other methods and classes here     &lt;br /&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;class&lt;/span&gt; Person     &lt;br /&gt;{     &lt;br /&gt;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt; FirstName {&lt;span style="color: blue"&gt;get&lt;/span&gt;;&lt;span style="color: blue"&gt;set&lt;/span&gt;;}     &lt;br /&gt;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt; LastName {&lt;span style="color: blue"&gt;get&lt;/span&gt;;&lt;span style="color: blue"&gt;set&lt;/span&gt;;}     &lt;br /&gt;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt; Email1 {&lt;span style="color: blue"&gt;get&lt;/span&gt;;&lt;span style="color: blue"&gt;set&lt;/span&gt;;}     &lt;br /&gt;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt; Email2 {&lt;span style="color: blue"&gt;get&lt;/span&gt;;&lt;span style="color: blue"&gt;set&lt;/span&gt;;}     &lt;br /&gt;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt; Phone1 {&lt;span style="color: blue"&gt;get&lt;/span&gt;;&lt;span style="color: blue"&gt;set&lt;/span&gt;;}     &lt;br /&gt;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt; Phone2 {&lt;span style="color: blue"&gt;get&lt;/span&gt;;&lt;span style="color: blue"&gt;set&lt;/span&gt;;}     &lt;br /&gt;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt; Phone3 {&lt;span style="color: blue"&gt;get&lt;/span&gt;;&lt;span style="color: blue"&gt;set&lt;/span&gt;;}     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;class&lt;/span&gt; PersonEqualityComparer : IEqualityComparer&amp;lt;Person&amp;gt;     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;bool&lt;/span&gt; Equals(Person x, Person y) {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; IsMatch(     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt;[]{x.Email1,x.Email2},     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt;[]{y.Email1,y.Email2}     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ) || IsMatch(     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt;[]{x.Phone1,x.Phone2,x.Phone3},     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt;[]{y.Phone1,y.Phone2,y.Phone3});     &lt;br /&gt;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;int&lt;/span&gt; GetHashCode(Person obj) {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; obj.GetHashCode();     &lt;br /&gt;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;bool&lt;/span&gt; IsMatch(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] words1,&lt;span style="color: blue"&gt;string&lt;/span&gt;[] words2) {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; (     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; words1.Where(w =&amp;gt; !String.IsNullOrEmpty( w ))     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Intersect(     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; words2.Where(w =&amp;gt; !String.IsNullOrEmpty( w ))     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ).Count() &amp;gt; 0);     &lt;br /&gt;&amp;#160;&amp;#160; }     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;Here are the results:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_rkddjaBL1xc/Sj6upxraykI/AAAAAAAAADQ/V0T9Wt2APNk/s1600-h/UnionAndDistinct3.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="UnionAndDistinct" border="0" alt="UnionAndDistinct" src="http://lh5.ggpht.com/_rkddjaBL1xc/Sj6urV2V_DI/AAAAAAAAADU/Ivx81ZUVa90/UnionAndDistinct_thumb1.jpg?imgmax=800" width="752" height="958" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;It is what you expected for the first two results (distinct and union is done directly in T-SQL but next two looks surprising. If I didn't use a new Person class then those would be same as the first two results. But we created a custom comparer to mean that:&lt;/p&gt;  &lt;p&gt;&amp;quot;If any email or phone matches then it is a duplicate&amp;quot; &lt;/p&gt;  &lt;p&gt;so why would we get too many results? Much more than the one without a custom comparison? Well, we defined a custom Equals() method but we still return the Hash Code of the original object. Remember to be accepted same not only should Equals() return true but hash codes must match as well. For this reason none of them is equal and union returns a result like as if we wanted to do a &amp;quot;union all&amp;quot;. To correct it we would just change the GetHashCode() implementation and see what we would get:&amp;#160; &lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;int&lt;/span&gt; GetHashCode(Person obj) {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt;&amp;#160;&lt;span style="color: red"&gt;&amp;quot;&amp;quot;&lt;/span&gt;.GetHashCode();     &lt;br /&gt;&amp;#160;&amp;#160; }&lt;/div&gt;  &lt;p&gt;and here are our new result sets (last two displayed):&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_rkddjaBL1xc/Sj6ur0Sw6TI/AAAAAAAAADY/BY5NazDN9qE/s1600-h/UnionAndDistinct23.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="UnionAndDistinct2" border="0" alt="UnionAndDistinct2" src="http://lh4.ggpht.com/_rkddjaBL1xc/Sj6usdXohKI/AAAAAAAAADc/qIpmEvaZQqE/UnionAndDistinct2_thumb1.jpg?imgmax=800" width="745" height="235" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Now that we have covered defining our own equality operator, Union and Distinct lets check other methods Concat,Intersect and Except.&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Customers    &lt;br /&gt;&amp;#160; .Where( c =&amp;gt; c.Country == &lt;span style="color: red"&gt;&amp;quot;USA&amp;quot;&lt;/span&gt; )     &lt;br /&gt;&amp;#160; .Select( c =&amp;gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; {c.CustomerID,c.CompanyName,c.Country} )     &lt;br /&gt;&amp;#160; .Concat(&amp;#160; &lt;br /&gt;Customers     &lt;br /&gt;&amp;#160; .Where( c =&amp;gt; c.Country == &lt;span style="color: red"&gt;&amp;quot;UK&amp;quot;&lt;/span&gt; )     &lt;br /&gt;&amp;#160; .Select( c =&amp;gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; {c.CustomerID,c.CompanyName,c.Country} )     &lt;br /&gt;) &lt;/div&gt;  &lt;p&gt;This sample is a typical &amp;quot;UNION ALL&amp;quot; of USA customers set and UK customers set ( Select( c =&amp;gt; new … ) projection is for sampling only, if we didn't have Select() parts it would be all fields).&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Products.Where( p =&amp;gt; p.OrderDetails.Any( od =&amp;gt; od.Order.Customer.Country == &lt;span style="color: red"&gt;&amp;quot;Argentina&amp;quot;&lt;/span&gt; )) &lt;/div&gt;  &lt;p&gt;Here we use Any() which we will see later in &amp;quot;Quantifier&amp;quot; operators. What it does is to return true if any item in the set has a match to its filter expression ( T-SQL exists or any). This query is selecting products that are bought by Argentina customers. Here is the result set (partial):&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_rkddjaBL1xc/Sj6usxnAqjI/AAAAAAAAADg/OJwXMKC6X-Y/s1600-h/ArgentinaPartial%5B3%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ArgentinaPartial" border="0" alt="ArgentinaPartial" src="http://lh3.ggpht.com/_rkddjaBL1xc/Sj6utrlWTfI/AAAAAAAAADk/GEjbf34R__8/ArgentinaPartial_thumb%5B1%5D.jpg?imgmax=800" width="413" height="427" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Products.Where( p =&amp;gt; p.OrderDetails.Any( od =&amp;gt; od.Order.Customer.Country == &lt;span style="color: red"&gt;&amp;quot;Argentina&amp;quot;&lt;/span&gt; ))     &lt;br /&gt;.Intersect(     &lt;br /&gt;Products.Where( p =&amp;gt; p.OrderDetails.Any( od =&amp;gt; od.Order.Customer.Country == &lt;span style="color: red"&gt;&amp;quot;Belgium&amp;quot;&lt;/span&gt; ))     &lt;br /&gt;) &lt;/div&gt;  &lt;p&gt;In this query Second part is doing the same for Belgium customers creating a seconds set. Then using INTERSECT we are getting the Products which are common to both sets. Here is the result set (picture cut for space):&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_rkddjaBL1xc/Sj6ut237tCI/AAAAAAAAADo/CGtT7H3rIRk/s1600-h/intersect%5B3%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="intersect" border="0" alt="intersect" src="http://lh6.ggpht.com/_rkddjaBL1xc/Sj6uuhpVeHI/AAAAAAAAADs/1O7o-M01WSQ/intersect_thumb%5B1%5D.jpg?imgmax=800" width="333" height="342" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;As you should notice there are 14 common items (Argentina customers bought 32 different products). Looking at the results we could say that products with Id 5, 7, 11, 13, 14, 16, 32,34 … are not bought by Belgium customers. Lets find out which products bought by Argentina customers were not on demand by Belgium customers (in other words: what are the products that are bought by Argentina customers EXCEPT those are also bought by Belgium customers). &lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Products.Where( p =&amp;gt; p.OrderDetails    &lt;br /&gt;&amp;#160; .Any( od =&amp;gt; od.Order.Customer.Country == &lt;span style="color: red"&gt;&amp;quot;Argentina&amp;quot;&lt;/span&gt; ))     &lt;br /&gt;.Except(     &lt;br /&gt;Products.Where( p =&amp;gt; p.OrderDetails     &lt;br /&gt;&amp;#160; .Any( od =&amp;gt; od.Order.Customer.Country == &lt;span style="color: red"&gt;&amp;quot;Belgium&amp;quot;&lt;/span&gt; ))     &lt;br /&gt;) &lt;/div&gt;  &lt;p&gt;Here is the result set:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_rkddjaBL1xc/Sj6uu6iYVSI/AAAAAAAAADw/vmnY-nVCr0k/s1600-h/except%5B3%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="except" border="0" alt="except" src="http://lh4.ggpht.com/_rkddjaBL1xc/Sj6uvcPIUCI/AAAAAAAAAD0/KHPXSPZhkAc/except_thumb%5B1%5D.jpg?imgmax=800" width="318" height="414" /&gt;&lt;/a&gt;&amp;#160; &lt;/p&gt;  &lt;p&gt;Like in Distinct and Union, Intersect, Except methods have support for additional custom comparer.&lt;/p&gt;  &lt;p&gt;PS: Because of years of working with SQL I happened to write insert statements for LinqTest database in T-SQL. Instead I could do the data insertion part using Linq. For example if you wanted add few more it would look like:&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;&lt;span style="color: blue"&gt;void&lt;/span&gt; Main()     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160; List&amp;lt;BadData&amp;gt; persons =&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt; List&amp;lt;BadData&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt; BadData {FirstName=&lt;span style="color: red"&gt;&amp;quot;Frank&amp;quot;&lt;/span&gt;,LastName=&lt;span style="color: red"&gt;&amp;quot;Smith&amp;quot;&lt;/span&gt;,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Email1=&lt;span style="color: red"&gt;&amp;quot;m1&amp;quot;&lt;/span&gt;,Email2=&lt;span style="color: red"&gt;&amp;quot;m2&amp;quot;&lt;/span&gt;,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Phone1=&lt;span style="color: red"&gt;&amp;quot;p1&amp;quot;&lt;/span&gt;,Phone2=&lt;span style="color: red"&gt;&amp;quot;p2&amp;quot;&lt;/span&gt;,Phone3=&lt;span style="color: red"&gt;&amp;quot;p3&amp;quot;&lt;/span&gt;},     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt; BadData {FirstName=&lt;span style="color: red"&gt;&amp;quot;Cetin&amp;quot;&lt;/span&gt;,LastName=&lt;span style="color: red"&gt;&amp;quot;Basoz&amp;quot;&lt;/span&gt;},     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt; BadData {FirstName=&lt;span style="color: red"&gt;&amp;quot;Fred&amp;quot;&lt;/span&gt;,LastName=&lt;span style="color: red"&gt;&amp;quot;Flintstone&amp;quot;&lt;/span&gt;,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Email1=&lt;span style="color: red"&gt;&amp;quot;fred@yellowstone.com&amp;quot;&lt;/span&gt;,Phone1=&lt;span style="color: red"&gt;&amp;quot;555-111 11 11&amp;quot;&lt;/span&gt;}     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; };     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160; BadData.InsertAllOnSubmit(persons);     &lt;br /&gt;&amp;#160;&amp;#160; BadData.Context.SubmitChanges();     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160; // Check results     &lt;br /&gt;&amp;#160;&amp;#160; BadData.Dump();     &lt;br /&gt;}&lt;/div&gt;  &lt;p&gt;Or you could insert a single element like this:&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;BadData b = &lt;span style="color: blue"&gt;new&lt;/span&gt; BadData();     &lt;br /&gt;b.FirstName = &lt;span style="color: red"&gt;&amp;quot;Jack&amp;quot;&lt;/span&gt;;     &lt;br /&gt;b.LastName = &lt;span style="color: red"&gt;&amp;quot;Smith&amp;quot;&lt;/span&gt;;     &lt;br /&gt;b.Phone2 = &lt;span style="color: red"&gt;&amp;quot;666-123 45 67&amp;quot;&lt;/span&gt;;     &lt;br /&gt;    &lt;br /&gt;BadData.InsertOnSubmit( b );     &lt;br /&gt;// more code &lt;span style="color: blue"&gt;if&lt;/span&gt; any     &lt;br /&gt;BadData.Context.SubmitChanges(); &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;See you on next post.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-1149745556501787599?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/1149745556501787599/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=1149745556501787599&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/1149745556501787599'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/1149745556501787599'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/06/linq-query-operators-part-vi.html' title='LINQ Query Operators – Part VI'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/_rkddjaBL1xc/Sj6urV2V_DI/AAAAAAAAADU/Ivx81ZUVa90/s72-c/UnionAndDistinct_thumb1.jpg?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-6724978136046366251</id><published>2009-06-05T03:23:00.001+03:00</published><updated>2009-06-05T03:23:34.676+03:00</updated><title type='text'>LINQ Operators – Part V</title><content type='html'>&lt;p&gt;&lt;strong&gt;Joining operators&lt;/strong&gt;:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Join&lt;/strong&gt;: If you remember from query methods join is not needed in the case of direct relations in the model (however local queries are faster with join). You can create inner and left joins using Join operator. Syntax is simple:&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Customers.Join(&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;// Customers&lt;/font&gt;     &lt;br /&gt;&amp;#160;&amp;#160; Orders,&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;// inner join Orders      &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160; c =&amp;gt; c.CustomerID,&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;// (Customers as c) on c.CustomerID      &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160; o =&amp;gt; o.CustomerID,&amp;#160;&amp;#160; &lt;font color="#008000"&gt;// (Orders as o) = o.OrderID&lt;/font&gt;     &lt;br /&gt;&amp;#160;&amp;#160; (c, o) =&amp;gt; &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;// local aliases – select c.CustomerID, o.OrderID, o.OrderDate&lt;/font&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c.CustomerID,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; o.OrderID,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; o.OrderDate     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } )&amp;#160; &lt;/div&gt;  &lt;div&gt;Is equivalent to (and generates) this SQL:&lt;/div&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;SELECT c.CustomerID, o.OrderID, o.OrderDate    &lt;br /&gt;FROM Customers c     &lt;br /&gt;INNER JOIN Orders o ON c.CustomerID = o.CustomerID &lt;/div&gt;  &lt;p&gt;You can use a custom equality comparer delegate as a parameter.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;GroupJoin&lt;/strong&gt;: GroupJoin is&amp;#160; a type of join that groups the results into lists based on join key – list of lists where inner lists are &amp;quot;grouped&amp;quot; on key.&amp;#160;&amp;#160; &lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 100%; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Customers.GroupJoin(    &lt;br /&gt;&amp;#160;&amp;#160; Orders,     &lt;br /&gt;&amp;#160;&amp;#160; c =&amp;gt; c.CustomerID,     &lt;br /&gt;&amp;#160;&amp;#160; o =&amp;gt; o.CustomerID,     &lt;br /&gt;&amp;#160;&amp;#160; (input,outputSequence) =&amp;gt; outputSequence     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Select( r =&amp;gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; { r.CustomerID, r.OrderID, r.OrderDate } ) ) &lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-6724978136046366251?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/6724978136046366251/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=6724978136046366251&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/6724978136046366251'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/6724978136046366251'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/06/linq-operators-part-v.html' title='LINQ Operators – Part V'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-529493649457925692</id><published>2009-05-31T23:14:00.001+03:00</published><updated>2009-05-31T23:14:20.038+03:00</updated><title type='text'>LINQ Operators – Part IV</title><content type='html'>&lt;p&gt;&lt;strong&gt;Grouping operators&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;GroupBy&lt;/strong&gt; : GroupBy is used to group a sequence into subsets. At first look it feels like it is same as T-SQL &amp;quot;Group By&amp;quot; but it is not. It slices a sequence into multiple &amp;quot;rows&amp;quot; where each row have a &amp;quot;key&amp;quot; and subset of sequence matching to those key. Here is a screen shot how it looks with this simple query:&lt;/p&gt;  &lt;div&gt;   &lt;div style="background-color: #e2ecf6"&gt;Customers.GroupBy( c =&amp;gt; c.Country )&lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_rkddjaBL1xc/SiLk9whuolI/AAAAAAAAACw/14Kj6KfbAHQ/s1600-h/groupBy%5B3%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="groupBy" border="0" alt="groupBy" src="http://lh3.ggpht.com/_rkddjaBL1xc/SiLk_tb2PZI/AAAAAAAAAC0/-gcTUr9y6fA/groupBy_thumb%5B1%5D.jpg?imgmax=800" width="673" height="440" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;GroupBy has an easy syntax with some overloads. You can optionally specify an element selector (projection) and also optionally specify a custom equality comparer. Equality comparer is less used and not different from the sample we used in OrderBy().&lt;/p&gt;  &lt;p&gt;Here is a sample with an element selector:&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 34.86%; height: 202px; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Customers    &lt;br /&gt;&amp;#160;&amp;#160; .GroupBy (     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c =&amp;gt; c.Country,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c =&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CustomerID = c.CustomerID,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CompanyName = c.CompanyName     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160; )&amp;#160; &lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;&lt;a href="http://lh5.ggpht.com/_rkddjaBL1xc/SiLlAuHQz8I/AAAAAAAAAC4/PrX1EKYrhC4/s1600-h/groupBySelector%5B3%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="groupBySelector" border="0" alt="groupBySelector" src="http://lh4.ggpht.com/_rkddjaBL1xc/SiLlB46WDCI/AAAAAAAAAC8/I1-POwUXLMY/groupBySelector_thumb%5B1%5D.jpg?imgmax=800" width="358" height="543" /&gt;&lt;/a&gt; &lt;/div&gt;  &lt;p&gt;   &lt;br /&gt;Latter GroupBy selects only two properties of Customers instead of all properties. You can also group by multiple keys:&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 47.01%; height: 287px; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Customers    &lt;br /&gt;&amp;#160;&amp;#160; .GroupBy (     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c =&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Country = c.Country,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Region = c.Region     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }, // keySelector     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c =&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CustomerID = c.CustomerID,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CompanyName = c.CompanyName     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160; //elementSelector     &lt;br /&gt;&amp;#160;&amp;#160; )&amp;#160; &lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_rkddjaBL1xc/SiLlClf2sLI/AAAAAAAAADA/2Fk5HPbKRMw/s1600-h/groupByMultipleKeys%5B3%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="groupByMultipleKeys" border="0" alt="groupByMultipleKeys" src="http://lh4.ggpht.com/_rkddjaBL1xc/SiLlEDOtvZI/AAAAAAAAADE/vV7ko1JUAzE/groupByMultipleKeys_thumb%5B1%5D.jpg?imgmax=800" width="359" height="617" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;In all these queries you are getting a sequence of &amp;quot;groupings&amp;quot;. You can &amp;quot;browse&amp;quot; the result with nested foreach() which is sort of nested scan…endscan:&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 45.77%; height: 258px; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;var byCountry = Customers.GroupBy ( c =&amp;gt; c.Country );    &lt;br /&gt;&amp;#160;&amp;#160; &lt;br /&gt;&lt;span style="color: blue"&gt;foreach&lt;/span&gt;(var g &lt;span style="color: blue"&gt;in&lt;/span&gt; byCountry)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160; Console.WriteLine(&lt;span style="color: red"&gt;&amp;quot;Country: {0}&amp;quot;&lt;/span&gt;, g.Key);     &lt;br /&gt;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;foreach&lt;/span&gt;(var customer &lt;span style="color: blue"&gt;in&lt;/span&gt; g)     &lt;br /&gt;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span style="color: red"&gt;&amp;quot;\t {0}\t{1,-50}\t{2}&amp;quot;&lt;/span&gt;,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; customer.CustomerID,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; customer.CompanyName,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; customer.ContactName);     &lt;br /&gt;&amp;#160;&amp;#160; }     &lt;br /&gt;}&amp;#160; &lt;/div&gt;  &lt;div&gt;Partial result:&lt;/div&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_rkddjaBL1xc/SiLlFRB1hxI/AAAAAAAAADI/9brlzUIqCD0/s1600-h/groupByMultipleKeys3%5B4%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="groupByMultipleKeys3" border="0" alt="groupByMultipleKeys3" src="http://lh5.ggpht.com/_rkddjaBL1xc/SiLlGg-sexI/AAAAAAAAADM/gCjJ8TdPAN0/groupByMultipleKeys3_thumb%5B2%5D.jpg?imgmax=800" width="573" height="471" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;In other words we are dealing with a sequence of sequences. With LinqToSQL inner sequences are queried as they are needed resulting in &amp;quot;groups count&amp;quot; + 1 selects send to backend. This may or may not be the behavior you want depending on where you need it (you can see the series of SQLs in LinqPad's SQL tab). To prevent this behavior you can convert Customers to an IEnumerable&amp;#160; from IQueryable (or to list, array…) first and then do grouping. For example:&lt;/p&gt;  &lt;p&gt;Customers.AsEnumerable().GroupBy( c =&amp;gt; c.Country ) &lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;AsEnumerable() is a conversion operator that converts the IQueryable to an IEnumerable ( in other words from a remote source to local source – similar to cursors ). We will use AsEnumerable() in some of the following samples that show statistics, nested grouping, distinct sequences.&lt;/p&gt;  &lt;p&gt;This sample shows some statistics about Customers grouped by country:&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 49.97%; height: 368px; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Orders.GroupBy (    &lt;br /&gt;&amp;#160; o =&amp;gt; o.OrderDate.GetValueOrDefault().Year )     &lt;br /&gt;.Select (     &lt;br /&gt;&amp;#160;&amp;#160; sales =&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Year = sales.Key,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Customers = sales     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Select( s =&amp;gt; s.CustomerID )     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Distinct()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Count(),     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Countries = sales     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Select( s =&amp;gt; s.ShipCountry )     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Distinct()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Count(),     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FirstSale = sales.Min( s =&amp;gt; s.OrderDate ),     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; LastSale&amp;#160; = sales.Max( s =&amp;gt; s.OrderDate )     &lt;br /&gt;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160; ).OrderBy( s =&amp;gt; s.Year )&amp;#160; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;This sample groups customerId and countries by years they made an &amp;quot;order&amp;quot;:&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 49%; height: 292px; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Orders.AsEnumerable()    &lt;br /&gt;&amp;#160; .GroupBy( o =&amp;gt; o.OrderDate.GetValueOrDefault().Year )     &lt;br /&gt;&amp;#160; .Select (     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; sales =&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Year = sales.Key,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Customers = sales     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Select( s =&amp;gt; s.CustomerID )     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Distinct(),     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Countries = sales     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Select( s =&amp;gt; s.ShipCountry )     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Distinct()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160; )&amp;#160; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;This sample is a multilevel grouping. It first groups customers by first letter of countries and then by countries:&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; width: 49.13%; height: 401px; overflow: scroll; border-top: black 1px solid; border-right: black 1px solid"&gt;Customers.AsEnumerable()    &lt;br /&gt;.GroupBy( c =&amp;gt; c.Country [0] )     &lt;br /&gt;.Select( c =&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c.Key,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; countries =     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c.GroupBy( cn =&amp;gt; cn.Country )     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Select( cs =&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cs.Key,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; clist =     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cs.Select( cf =&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cf.CompanyName,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cf.Country,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cf.Region})     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }).OrderBy( cn =&amp;gt; cn.Key )     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }).OrderBy( c =&amp;gt; c.Key ) &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;There is another grouping operator called ToLookup() that creates one to many dictionary. I reserved it into another category.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-529493649457925692?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/529493649457925692/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=529493649457925692&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/529493649457925692'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/529493649457925692'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/05/linq-operators-part-iv.html' title='LINQ Operators – Part IV'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/_rkddjaBL1xc/SiLk_tb2PZI/AAAAAAAAAC0/-gcTUr9y6fA/s72-c/groupBy_thumb%5B1%5D.jpg?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-3319004488674716348</id><published>2009-05-28T02:55:00.001+03:00</published><updated>2009-05-28T02:55:54.417+03:00</updated><title type='text'>LINQ Query Operators – Part III</title><content type='html'>&lt;p&gt;&lt;strong&gt;Ordering Operators:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Ordering operators like T-SQL &amp;quot;Order By&amp;quot; is used for ordering sequences in different ways. There are 5 ordering operators:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;OrderBy &lt;/li&gt;    &lt;li&gt;OrderByDescending &lt;/li&gt;    &lt;li&gt;ThenBy &lt;/li&gt;    &lt;li&gt;ThenByDescending &lt;/li&gt;    &lt;li&gt;Reverse &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Except the last operator Reverse() all those are used as:&lt;/p&gt;  &lt;p&gt;operator( keySelector ) or operator( keySelector, customComparer )&amp;#160; - customComparer is for custom sorting and not supported by L2S (Reverse() is not supported either).&lt;/p&gt;  &lt;p&gt;You should have noticed that &amp;quot;descending&amp;quot; is not a parameter but separate methods exist to sort descending. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;OrderBy:&lt;/strong&gt; Orders the sequence by a given &amp;quot;keySelector&amp;quot;. i.e:&lt;/p&gt;  &lt;table border="1" cellspacing="0" cellpadding="2" width="583"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="279"&gt;         &lt;div&gt;Linq &lt;/div&gt;       &lt;/td&gt;        &lt;td valign="top" width="302"&gt;T-SQL&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="279"&gt;         &lt;div&gt;Customers.OrderBy( c =&amp;gt; c.ContactName ) &lt;/div&gt;       &lt;/td&gt;        &lt;td valign="top" width="302"&gt;         &lt;div&gt;&lt;span style="color: blue"&gt;Select&lt;/span&gt; * from Customers order by ContactName &lt;/div&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Seconds overload accepts a comparer. Here is a sample that sorts orders by :&lt;/p&gt;  &lt;div&gt;&lt;font color="#008080"&gt;&lt;font color="#008000"&gt;// In LinqPad &lt;span style="color: blue"&gt;select&lt;/span&gt; C# Program &lt;span style="color: blue"&gt;from&lt;/span&gt; language combo         &lt;br /&gt;// Delete &lt;span style="color: blue"&gt;default&lt;/span&gt; code         &lt;br /&gt;// Copy Paste or better hands on, write yourself         &lt;br /&gt;// and press F5 to run&lt;/font&gt;       &lt;br /&gt;&lt;/font&gt;&lt;span style="color: blue"&gt;void&lt;/span&gt; Main()     &lt;br /&gt;{     &lt;br /&gt;&lt;font color="#008000"&gt;// This overload &lt;span style="color: blue"&gt;is&lt;/span&gt; not supported by L2S       &lt;br /&gt;// Getting &lt;span style="color: blue"&gt;into&lt;/span&gt; a local list first       &lt;br /&gt;// We would review later what does &lt;span style="color: red"&gt;&amp;quot;local&amp;quot;&lt;/span&gt; mean       &lt;br /&gt;// &lt;span style="color: blue"&gt;for&lt;/span&gt; now just consider queries on ordList&amp;#160; &lt;br /&gt;// &lt;span style="color: blue"&gt;is&lt;/span&gt; not Linq To SQL&amp;#160; &lt;br /&gt;// (sort of you are querying a local cursor which       &lt;br /&gt;// you created by a remote call to SQL server)&lt;/font&gt;&amp;#160; &lt;br /&gt;var ordList = Orders.Select( o =&amp;gt;&amp;#160; &lt;br /&gt;&lt;span style="color: blue"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new&lt;/span&gt; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; OrderId = o.OrderID,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; OrderDate = o.OrderDate     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }).ToList();     &lt;br /&gt;&amp;#160; &lt;br /&gt;&lt;font color="#008000"&gt;// creating an instance of custom comparer      &lt;br /&gt;// and passing it &lt;span style="color: blue"&gt;as&lt;/span&gt; a parameter       &lt;br /&gt;// final Select projection &lt;span style="color: blue"&gt;is&lt;/span&gt; about showing       &lt;br /&gt;// the result of sort       &lt;br /&gt;// Note that the sort &lt;span style="color: blue"&gt;is&lt;/span&gt; by &lt;span style="color: red"&gt;&amp;quot;Day Of Week&amp;quot;&lt;/span&gt; enumeration       &lt;br /&gt;// and not by &lt;span style="color: red"&gt;&amp;quot;WeekDay&amp;quot;&lt;/span&gt; alphabetically&lt;/font&gt;     &lt;br /&gt;var myCustomList =&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ordList     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .OrderBy( o =&amp;gt;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; o.OrderDate,&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt; OrderComparer() )&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Select( o =&amp;gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; o.OrderId,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; o.OrderDate,&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; o.OrderDate.GetValueOrDefault().DayOfWeek} );     &lt;br /&gt;    &lt;br /&gt;myCustomList.Dump();     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;&lt;font color="#008000"&gt;// Here &lt;span style="color: blue"&gt;is&lt;/span&gt; our custom sorting &lt;span style="color: blue"&gt;class&lt;/span&gt;       &lt;br /&gt;// It accepts parameters of type nullable DateTime       &lt;br /&gt;// and returns comparison result based on       &lt;br /&gt;// DayOfWeek – like DOW() &lt;span style="color: blue"&gt;in&lt;/span&gt; VFP&lt;/font&gt;     &lt;br /&gt;    &lt;br /&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;class&lt;/span&gt; OrderComparer : IComparer&amp;lt;DateTime?&amp;gt;     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;int&lt;/span&gt; Compare(DateTime? x, DateTime? y)     &lt;br /&gt;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; x.GetValueOrDefault().DayOfWeek     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .CompareTo(&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; y.GetValueOrDefault().DayOfWeek );&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;OrderByDescending:&lt;/strong&gt; Same as OrderBy() except that it sorts in descending order (it was obvious,isn't it).&lt;/p&gt;  &lt;p&gt;ThenBy, ThnenByDescending: ThenBy and ThenByDescending are used to sort on multiple columns. Hmm why would you need something like this? Can't you simply use chain of OrderBy(), OrderByDescending() operators? Of course you can but I think these were added to prevent you from writing it the way you intended (like in natural English you would be saying order by X then by Y then by Z). Probably this is best explained by a sample. We want to sort customers by their country and then by company name. It is very easy to write it as in first query and fail to get what we really intended. &lt;/p&gt;  &lt;table border="1" cellspacing="0" cellpadding="2" width="762"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="378"&gt;&lt;strong&gt;Linq&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="382"&gt;&lt;strong&gt;Corresponding SQL&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="378"&gt;         &lt;div&gt;Customers            &lt;br /&gt;&amp;#160;&amp;#160; .OrderBy(c =&amp;gt; c.Country)             &lt;br /&gt;&amp;#160;&amp;#160; .OrderBy(c =&amp;gt; c.CompanyName)&lt;/div&gt;          &lt;div&gt;&amp;#160;&amp;#160; .Select( c =&amp;gt; new {c.Country, c.CompanyName} )&lt;/div&gt;       &lt;/td&gt;        &lt;td valign="top" width="382"&gt;         &lt;div&gt;&lt;span style="color: blue"&gt;Select&lt;/span&gt; [t0].[Country], [t0].[CompanyName]             &lt;br /&gt;FROM [Customers] &lt;span style="color: blue"&gt;As&lt;/span&gt; [t0]             &lt;br /&gt;ORDER BY [t0].[CompanyName], [t0].[Country] &lt;/div&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="378"&gt;         &lt;div&gt;Customers            &lt;br /&gt;&amp;#160;&amp;#160; .OrderBy(c =&amp;gt; c.CompanyName)             &lt;br /&gt;&amp;#160;&amp;#160; .OrderBy(c =&amp;gt; c.Country) &lt;/div&gt;          &lt;div&gt;&amp;#160;&amp;#160; .Select( c =&amp;gt; new {c.Country, c.CompanyName} )&lt;/div&gt;       &lt;/td&gt;        &lt;td valign="top" width="382"&gt;         &lt;p&gt;SELECT [t0].[Country], [t0].[CompanyName]            &lt;br /&gt;FROM [Customers] AS [t0]             &lt;br /&gt;ORDER BY [t0].[Country], [t0].[CompanyName]&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="378"&gt;         &lt;div&gt;Customers            &lt;br /&gt;&amp;#160;&amp;#160; .OrderBy( c =&amp;gt; c.Country)             &lt;br /&gt;&amp;#160;&amp;#160; .ThenBy&amp;#160; ( c =&amp;gt; c.CompanyName)&lt;/div&gt;          &lt;div&gt;&amp;#160;&amp;#160; .Select( c =&amp;gt; new {c.Country, c.CompanyName} )&lt;/div&gt;       &lt;/td&gt;        &lt;td valign="top" width="382"&gt;         &lt;p&gt;SELECT [t0].[Country], [t0].[CompanyName]            &lt;br /&gt;FROM [Customers] AS [t0]             &lt;br /&gt;ORDER BY [t0].[Country], [t0].[CompanyName]&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;In SQL the order of columns dictate it and it is easy to write exactly what we want ( order by country and then by company ). However in Linq method syntax, keep in mind with each and every chained method we get a new intermediate sequence as a result ( and you could see how a series of methods is working by attaching a &amp;quot;Dump()&amp;quot; to each of those methods ). i.e:&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div&gt;Customers    &lt;br /&gt;&amp;#160; .Take(10).Dump(&lt;span style="color: red"&gt;&amp;quot;Got top 10&amp;quot;&lt;/span&gt;)     &lt;br /&gt;&amp;#160; .Select( c =&amp;gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; {c.Country, c.CompanyName} ).Dump(&lt;span style="color: red"&gt;&amp;quot;Projected to 2 columns: Country,Company&amp;quot;&lt;/span&gt;)     &lt;br /&gt;&amp;#160; .OrderBy( c =&amp;gt; c.Country).Dump(&lt;span style="color: red"&gt;&amp;quot;Sorted on Country&amp;quot;&lt;/span&gt;)     &lt;br /&gt;&amp;#160; .OrderBy( c =&amp;gt; c.CompanyName).Dump(&lt;span style="color: red"&gt;&amp;quot;Sorted on Company&amp;quot;&lt;/span&gt;) &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;As you can see we are first sorting on country and &amp;quot;then by&amp;quot; without a ThenBy() operator on company. The net effect we are sorting on company name only. ThenBy() operator excellently translates our intention about &amp;quot;by Country&amp;#160; THEN BY&amp;#160; Company Name&amp;quot;. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Reverse:&lt;/strong&gt; On a local sequence reverses the sequence as its name suggest. i.e:&lt;/p&gt;  &lt;div&gt;&lt;span style="color: blue"&gt;int&lt;/span&gt;[] numbers = {1,2,3,4,5};     &lt;br /&gt;numbers.Reverse().Dump();     &lt;br /&gt;    &lt;br /&gt;&lt;span style="color: blue"&gt;string&lt;/span&gt; reversed = &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt;(     &lt;br /&gt;&amp;#160; &lt;span style="color: red"&gt;&amp;quot;Reverse Me&amp;quot;&lt;/span&gt;.Reverse().ToArray()     &lt;br /&gt;&amp;#160; );     &lt;br /&gt;    &lt;br /&gt;Console.WriteLine(reversed); &lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-3319004488674716348?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/3319004488674716348/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=3319004488674716348&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/3319004488674716348'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/3319004488674716348'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/05/linq-query-operators-part-iii.html' title='LINQ Query Operators – Part III'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-2267706367210568255</id><published>2009-05-27T02:26:00.001+03:00</published><updated>2009-05-27T02:26:02.382+03:00</updated><title type='text'>LINQ Query Operators – Part II</title><content type='html'>&lt;p&gt;&lt;strong&gt;Projection Operators:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Select:&lt;/strong&gt; Selects items from a source, projecting the results as necessary.&lt;/p&gt;  &lt;p&gt;When the input sequence would be selected as is without any projection ( aka * in T-SQL ) using Select() is not necessary and looks a little ugly. i.e.:&lt;/p&gt;  &lt;div&gt;Customers.Select( c =&amp;gt; c )&lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt; &lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;  &lt;p&gt;Instead we would simply write just &amp;quot;Customers&amp;quot; which is the enumerable itself. In query syntax it is the same as writing: &lt;/p&gt;  &lt;div&gt;&lt;span style="color: blue"&gt;from&lt;/span&gt; c &lt;span style="color: blue"&gt;in&lt;/span&gt; Customers &lt;span style="color: blue"&gt;select&lt;/span&gt; c&lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;Here are some Select projections and its Query expression, T-SQL counterparts:&lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;table border="1" cellspacing="0" cellpadding="2" width="577"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="137"&gt;T-SQL&lt;/td&gt;        &lt;td valign="top" width="438"&gt;         &lt;div&gt;Select CompanyName &lt;span style="color: blue"&gt;from&lt;/span&gt; customers &lt;/div&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="137"&gt;Query Syntax&lt;/td&gt;        &lt;td valign="top" width="438"&gt;         &lt;div&gt;&lt;span style="color: blue"&gt;from&lt;/span&gt; c &lt;span style="color: blue"&gt;in&lt;/span&gt; Customers             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;select&lt;/span&gt; c.CompanyName&lt;/div&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="137"&gt;Method Syntax&lt;/td&gt;        &lt;td valign="top" width="438"&gt;         &lt;div&gt;Customers.Select( c =&amp;gt; c.CompanyName ) &lt;/div&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;table border="1" cellspacing="0" cellpadding="2" width="577"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="137"&gt;T-SQL&lt;/td&gt;        &lt;td valign="top" width="438"&gt;         &lt;div&gt;&lt;span style="color: blue"&gt;Select&lt;/span&gt;&amp;#160; &lt;br /&gt;&amp;#160; firstName, lastName,             &lt;br /&gt;&amp;#160; firstName +&amp;#160; ' ' + lastName &lt;span style="color: blue"&gt;As&lt;/span&gt; fullName             &lt;br /&gt;from employees &lt;/div&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="137"&gt;Query Syntax&lt;/td&gt;        &lt;td valign="top" width="438"&gt;         &lt;div&gt;&lt;span style="color: blue"&gt;from&lt;/span&gt; e &lt;span style="color: blue"&gt;in&lt;/span&gt; Employees             &lt;br /&gt;&lt;span style="color: blue"&gt;select&lt;/span&gt;&amp;#160; &lt;br /&gt;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.FirstName,             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.LastName,             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; fullName = e.FirstName + &lt;span style="color: red"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + e.LastName }&lt;/div&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="137"&gt;Method Syntax&lt;/td&gt;        &lt;td valign="top" width="438"&gt;         &lt;div&gt;Employees            &lt;br /&gt;&amp;#160; .Select( e =&amp;gt;             &lt;br /&gt;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.FirstName,             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.LastName,             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; fullName = e.FirstName + &lt;span style="color: red"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + e.LastName } ) &lt;/div&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;There is another overload of Select() which accepts an index parameter and is not supported by Linq To SQL and EF. Here is a simple example that lists the DBF files from VFP samples data folder enumerating them with their position (reminder: in C# indexing, arrays start from 0):&lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;Directory.GetFiles(    &lt;br /&gt;&amp;#160;&amp;#160; @&lt;span style="color: red"&gt;&amp;quot;c:\Program Files\Microsoft Visual Foxpro 9\Samples\Data&amp;quot;&lt;/span&gt;,&amp;#160; &lt;span style="color: red"&gt;&amp;quot;*.dbf&amp;quot;&lt;/span&gt;)     &lt;br /&gt;&amp;#160;&amp;#160; .Select( (filename,position) =&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt; {fileNo = position+1,filename} ) &lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;In the samples above, with the &amp;quot;new&amp;quot; keyword the type created is an anonymous type. Sometimes we would want to create a type of our own (similar to &amp;quot;into cursor cursorName&amp;quot; – cursorName might a different structure than the source). To do that we could create a new class and qualify the &amp;quot;new&amp;quot; type with that class. i.e.:&lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;&lt;span style="color: blue"&gt;void&lt;/span&gt; Main()     &lt;br /&gt;{     &lt;br /&gt;&amp;#160; var q = &lt;span style="color: blue"&gt;from&lt;/span&gt; c &lt;span style="color: blue"&gt;in&lt;/span&gt; Customers     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;select&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;new&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; myCustomer {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ID = c.CustomerID,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; company = c.CompanyName,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; orderCount = c.Orders.Count     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; };     &lt;br /&gt;    &lt;br /&gt;&amp;#160; q.Dump();     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;class&lt;/span&gt; myCustomer     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt; ID {&lt;span style="color: blue"&gt;get&lt;/span&gt;;&lt;span style="color: blue"&gt;set&lt;/span&gt;;}     &lt;br /&gt;&amp;#160;&lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;string&lt;/span&gt; company {&lt;span style="color: blue"&gt;get&lt;/span&gt;;&lt;span style="color: blue"&gt;set&lt;/span&gt;;}     &lt;br /&gt;&amp;#160;&lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;int&lt;/span&gt; orderCount {&lt;span style="color: blue"&gt;get&lt;/span&gt;;&lt;span style="color: blue"&gt;set&lt;/span&gt;;}     &lt;br /&gt;}     &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;This produces the SQL:&lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;p&gt;SELECT    &lt;br /&gt;&amp;#160; [t0].[CustomerID] AS [ID],     &lt;br /&gt;&amp;#160; [t0].[CompanyName] AS [company],     &lt;br /&gt;&amp;#160; (     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; SELECT COUNT(*)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; FROM [Orders] AS [t1]     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; WHERE [t1].[CustomerID] = [t0].[CustomerID]     &lt;br /&gt;&amp;#160; ) AS [orderCount]     &lt;br /&gt;FROM [Customers] AS [t0]&lt;/p&gt;  &lt;div&gt;&lt;strong&gt;SelectMany:&lt;/strong&gt; The Select() method works as Select( input =&amp;gt;output ) where it produces one output per input. Consider a query getting customers and orders of customers (maybe customerID and CompanyName from Customers and OrderID, OrderDate from orders to show simpler output):&lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;&lt;span style="color: blue"&gt;Select&lt;/span&gt;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160; [t0].[CustomerID], [t0].[CompanyName], [t1].[OrderID] &lt;span style="color: blue"&gt;As&lt;/span&gt; [OrderID], [t1].[OrderDate]     &lt;br /&gt;FROM [Customers] &lt;span style="color: blue"&gt;As&lt;/span&gt; [t0], [Orders] &lt;span style="color: blue"&gt;As&lt;/span&gt; [t1]     &lt;br /&gt;WHERE [t1].[CustomerID] = [t0].[CustomerID] &lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;This trivial SQL generates a 2D flat table.&amp;#160; If we attempt to create this with a Select() we could do that using an expression like below:&lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;p&gt;Orders.Select( o =&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160; new&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; o.Customer.CustomerID,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; o.Customer.CompanyName,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; o.OrderID,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; o.OrderDate     &lt;br /&gt;&amp;#160;&amp;#160; } )&lt;/p&gt;  &lt;div&gt;Which produce this SQL:&lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;&lt;span style="color: blue"&gt;Select&lt;/span&gt;&amp;#160; &lt;br /&gt;[t1].[CustomerID], [t1].[CompanyName],&amp;#160; &lt;br /&gt;[t0].[OrderID], [t0].[OrderDate]     &lt;br /&gt;FROM [Orders] &lt;span style="color: blue"&gt;As&lt;/span&gt; [t0]     &lt;br /&gt;LEFT OUTER JOIN [Customers] &lt;span style="color: blue"&gt;As&lt;/span&gt; [t1]&amp;#160; &lt;br /&gt;&lt;span style="color: blue"&gt;On&lt;/span&gt; [t1].[CustomerID] = [t0].[CustomerID] &lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;and the flat 2D result that we are accustomed to. If you look at the SQL closely you would notice that it is sort of cheat creating an inner join (Orders driving the results and already each Order has a &amp;quot;parent&amp;quot; Customer, this in effect is an inner join). We could also look at it as:&lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;use Customers order tag CustomerID&lt;/div&gt;  &lt;div&gt;select 0&lt;/div&gt;  &lt;div&gt;use Orders&lt;/div&gt;  &lt;div&gt;set relation to CustomerID into Customers&lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;The old Indian trick that we used in reports in old days to get all orders of customers using relations. If we did the relation from Customers into Orders and then browse each table side by side remember per Customer in left browse we would see &amp;quot;Many&amp;quot; Orders on right. Or better yet we all wanted to have Grid in Grid or hierarchical grids showing one Customer in a row and her Orders in a grid in another grid cell. Doing that is not trivial except using relations and that would be a single &amp;quot;thing&amp;quot;. Now that we are in objects world a single object would be representing a data structure where per customer &amp;quot;row&amp;quot; there are multiple order &amp;quot;rows&amp;quot; (think of a report where data is grouped on CustomerID). If we have started from Customers (and remember we get one output per input) we would end up with such an &amp;quot;object&amp;quot; which is an ordered enumerable of Customers along with its Orders. Here is our new query:&lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;Customers.Select( c =&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c.CustomerID,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c.CompanyName,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; OrderList = c.Orders.Select( o =&amp;gt;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt; {o.OrderID, o.OrderDate})     &lt;br /&gt;&amp;#160;&amp;#160; } ) &lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;Producing SQL:&lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;&lt;span style="color: blue"&gt;Select&lt;/span&gt;&amp;#160; &lt;br /&gt;&amp;#160; [t0].[CustomerID],&amp;#160; &lt;br /&gt;&amp;#160; [t0].[CompanyName],&amp;#160; &lt;br /&gt;&amp;#160; [t1].[OrderID], [t1].[OrderDate],&amp;#160; &lt;br /&gt;&amp;#160; (     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;Select&lt;/span&gt; COUNT(*)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; FROM [Orders] &lt;span style="color: blue"&gt;As&lt;/span&gt; [t2]     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; WHERE [t2].[CustomerID] = [t0].[CustomerID]     &lt;br /&gt;&amp;#160; ) &lt;span style="color: blue"&gt;As&lt;/span&gt; [value]     &lt;br /&gt;FROM [Customers] &lt;span style="color: blue"&gt;As&lt;/span&gt; [t0]     &lt;br /&gt;&amp;#160; LEFT OUTER JOIN [Orders] &lt;span style="color: blue"&gt;As&lt;/span&gt; [t1]     &lt;br /&gt;&amp;#160; &lt;span style="color: blue"&gt;On&lt;/span&gt; [t1].[CustomerID] = [t0].[CustomerID]     &lt;br /&gt;ORDER BY [t0].[CustomerID], [t1].[OrderID] &lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;If we run this SQL we would get a flat list as always. However the Linq query returns an object result which is more like &amp;quot;parent-child&amp;quot; packed data (like a 3D table). Here is part of output from LinqPad to understand better (and I hope you are following these testing in LinqPad yourself):&lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;&lt;a href="http://lh6.ggpht.com/_rkddjaBL1xc/Shx6hN0xpPI/AAAAAAAAACo/ws7Lb2csB6s/s1600-h/Select%5B3%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Select" border="0" alt="Select" src="http://lh5.ggpht.com/_rkddjaBL1xc/Shx6iPsggBI/AAAAAAAAACs/KXaCb7OAVpc/Select_thumb%5B1%5D.jpg?imgmax=800" width="569" height="386" /&gt;&lt;/a&gt; &lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;This is more like grouping that we may like. We may want this as a flat 2D as we are accustomed to and SelectMany() comes into play there.&amp;#160; With query syntax it is as easy as:&lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;&lt;span style="color: blue"&gt;from&lt;/span&gt; c &lt;span style="color: blue"&gt;in&lt;/span&gt; Customers     &lt;br /&gt;&lt;span style="color: blue"&gt;from&lt;/span&gt; o &lt;span style="color: blue"&gt;in&lt;/span&gt; c.Orders     &lt;br /&gt;&lt;span style="color: blue"&gt;select&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;new&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c.CustomerID,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c.CompanyName,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; o.OrderID,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; o.OrderDate     &lt;br /&gt;&amp;#160;&amp;#160; } &lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;this is converted to SelectMany() as shown here:&lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;Customers    &lt;br /&gt;&amp;#160;&amp;#160; .SelectMany (     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c =&amp;gt; c.Orders,&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (c, o) =&amp;gt;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CustomerID = c.CustomerID,&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CompanyName = c.CompanyName,&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; OrderID = o.OrderID,&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; OrderDate = o.OrderDate     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160; )     &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;This concludes projection operators subject. &lt;/div&gt;  &lt;div&gt;&amp;#160;&lt;/div&gt;  &lt;div&gt;Note: I intentionally didn't mention some details about operators shown and left out some operator(s) that may be part of categories we talked about in order to prevent confusion. Also note that these are standard operators. Linq is evolving and already there are many more operators than you would see in standard operators set. Trying to keep it simple and summarized I might be skipping details more than I should, I hope not. &lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-2267706367210568255?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/2267706367210568255/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=2267706367210568255&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/2267706367210568255'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/2267706367210568255'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/05/linq-query-operators-part-ii.html' title='LINQ Query Operators – Part II'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/_rkddjaBL1xc/Shx6iPsggBI/AAAAAAAAACs/KXaCb7OAVpc/s72-c/Select_thumb%5B1%5D.jpg?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-7861549233019180620</id><published>2009-05-24T05:20:00.001+03:00</published><updated>2009-05-24T05:20:29.844+03:00</updated><title type='text'>LINQ Query Operators – Part I</title><content type='html'>&lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;Orders.OrderBy(o =&amp;gt; o.OrderDate).Skip(5)&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Before checking the operators lets remind some points.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;Query operators are extension methods &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;Some Linq operations are only available with operators (or to say it otherwise &amp;quot;method syntax&amp;quot;) &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;Syntax includes the source itself which need not be specified (because implemented as static extension method on IEnumerable, IOW source). Typical syntax looks like: &lt;br /&gt;    &lt;br /&gt;public static IEnumerable&amp;lt;TSource&amp;gt; Where&amp;lt;TSource&amp;gt;( this IEnumerable&amp;lt;TSource&amp;gt; source, Func&amp;lt;TSource, Boolean&amp;gt; predicate) &lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Looks like Chinese? It is very easy once you understand indeed. You can think of it as:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#ff0000"&gt;IEnumerable&amp;lt;TSource&amp;gt;&lt;/font&gt; : It returns something that can be enumerated ( like an array, list, records …) - and its type is symbolically TSource.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#ff0000"&gt;Where&amp;lt;TSource&amp;gt;( this IEnumerable&amp;lt;TSource&amp;gt; source&lt;/font&gt; :&amp;#160; First parameter is also something that can be enumerated and since it is an extension method of IEnumerable itself we don't need to feed source parameter.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Up to this point it is simple, gets something like a list and returns back another list. Just like an SQL select does. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#ff0000"&gt;Func&amp;lt;TSource, Boolean&amp;gt; predicate)&lt;/font&gt; : Here comes the interesting part. It accepts a &amp;quot;function&amp;quot; that has the TSource type input parameter (you can think of TSource as a row of customer when the source is customers table) and returns back true or false. If true is returned &amp;quot;record&amp;quot; is selected.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Now lets check this &amp;quot;Where&amp;quot; using in a Linq query and compare to its counterpart SQL select and Linq query syntax:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;SQL select: &lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;select&lt;/span&gt; * &lt;span class="kwrd"&gt;from&lt;/span&gt; Customers &lt;span class="kwrd"&gt;where&lt;/span&gt; Customers.Country = &amp;quot;USA&amp;quot; &lt;span class="kwrd"&gt;into&lt;/span&gt; &lt;span class="kwrd"&gt;cursor&lt;/span&gt; usaCustomers&lt;/pre&gt;&lt;br /&gt;LINQ Query Syntax: &lt;br /&gt;&lt;br /&gt;  &lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;IEnumerable&amp;lt;Customers&amp;gt; usaCustomers = from c &lt;span class="kwrd"&gt;in&lt;/span&gt; Customers &lt;span class="kwrd"&gt;where&lt;/span&gt; c.Country == &lt;span class="str"&gt;&amp;quot;USA&amp;quot;&lt;/span&gt; select c;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;LINQ Method Syntax: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;IEnumerable&amp;lt;Customers&amp;gt; usaCustomers = Customers.Where( c =&amp;gt; c.Country == &lt;span class="str"&gt;&amp;quot;USA&amp;quot;&lt;/span&gt; );&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Lets dissect the method syntax and see different ways of writing the same thing:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;Customers.Where( c =&amp;gt; c.Country == &lt;span class="str"&gt;&amp;quot;USA&amp;quot;&lt;/span&gt; );&lt;/pre&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;TSource is Customers. In method Where(), c is the input parameter to function (remember it represents something like a Customers row, so it has columns as properties). it returns the result of check: c.Country == &amp;quot;USA&amp;quot; which is a true or false. We could write the same thing like this:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;Customers.Where( ( c ) =&amp;gt; c.Country == &lt;span class="str"&gt;&amp;quot;USA&amp;quot;&lt;/span&gt; );&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;When parameter count is one, we wouldn't want to do that however and simply use the previous syntax. When parameter count is multiple we put them within parentheses. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;For readability we are allowed to write the exact same thing like this (the newline and extra whitespace between Customers and .Where is insignificant):&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;IEnumerable&amp;lt;Customers&amp;gt; usaCustomers = Customers&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;                                      .Where( c =&amp;gt; c.Country == &lt;span class="str"&gt;&amp;quot;USA&amp;quot;&lt;/span&gt; );&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&amp;#160;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Now that I think we are armed with basic syntax we can move on. LINQ operators are divided into categories. Members of categories may be different&amp;#160; from source to source but in general all sources have same categories (and some operators are listed in more than one category).&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Filtering and Partitioning Operators: &lt;br /&gt;    &lt;br /&gt;Where:&lt;/strong&gt;&amp;#160; This is the filtering operator which any VFP developer would be most familiar with. It has two forms &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;  &lt;li&gt;Typical function getting a source and returning a boolean (filters out for the ones returning false) &lt;br /&gt;    &lt;div class="csharpcode"&gt;&lt;br /&gt;      &lt;pre class="alt"&gt;var usaCustomers = Customers.Where( c =&amp;gt; c.Country == &lt;span class="str"&gt;&amp;quot;USA&amp;quot;&lt;/span&gt; );&lt;/pre&gt;&lt;br /&gt;    &lt;/div&gt;&lt;br /&gt;    &lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;A function getting an index parameter besides source (this one is not supported with Linq To SQL or Linq to Entity Framework because there isn't a supporting SQL for this. Practically speaking methods that do not a SQL counterpart do not work with L2S). &lt;br /&gt;    &lt;div class="csharpcode"&gt;&lt;br /&gt;      &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;int&lt;/span&gt;[] numbers = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17};&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;      &lt;pre&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;      &lt;pre class="alt"&gt;var some = numbers&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;      &lt;pre&gt;    .Where((c, index) =&amp;gt; ( &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;      &lt;pre class="alt"&gt;                         (c%2 == 0) &amp;amp;&amp;amp;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;      &lt;pre&gt;                         (index &amp;gt;= 8) &amp;amp;&amp;amp; (index &amp;lt;= 15)));&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;      &lt;pre class="alt"&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;      &lt;pre&gt;some.Dump(&lt;span class="str"&gt;&amp;quot;Even numbers between positions 8-15&amp;quot;&lt;/span&gt;);&lt;/pre&gt;&lt;br /&gt;    &lt;/div&gt;&lt;br /&gt;    &lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;1st syntax simply select customers from USA. 2nd syntax limits the &amp;quot;records&amp;quot; to position (sort of recno()) to 8-15 and within that range select numbers that are even.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Skip:&lt;/strong&gt; Skips N elements. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;int&lt;/span&gt;[] numbers = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17};&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;var some = numbers.Skip(5);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;some.Dump(&lt;span class="str"&gt;&amp;quot;Skipped first 5 numbers&amp;quot;&lt;/span&gt;);&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;In T-SQL it looks like: &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;select&lt;/span&gt; number &lt;span class="kwrd"&gt;from&lt;/span&gt; &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;(&lt;span class="kwrd"&gt;select&lt;/span&gt; number,Row_Number() &lt;span class="kwrd"&gt;over&lt;/span&gt; (&lt;span class="kwrd"&gt;order&lt;/span&gt; &lt;span class="kwrd"&gt;by&lt;/span&gt; number) &lt;span class="kwrd"&gt;as&lt;/span&gt; &lt;span class="kwrd"&gt;row&lt;/span&gt; &lt;span class="kwrd"&gt;from&lt;/span&gt; numbers) numberList&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;where&lt;/span&gt; &lt;span class="kwrd"&gt;row&lt;/span&gt; &amp;gt; 5 &lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Actually if you run this against Northwind:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;Orders.OrderBy(o =&amp;gt; o.OrderDate).Skip(5)&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This is the generated T-SQL code:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="rem"&gt;-- Region Parameters&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="kwrd"&gt;DECLARE&lt;/span&gt; @p0 &lt;span class="kwrd"&gt;Int&lt;/span&gt; &lt;span class="kwrd"&gt;SET&lt;/span&gt; @p0 = 5&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="rem"&gt;-- EndRegion&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; [t1].[OrderID], [t1].[CustomerID], [t1].[EmployeeID],&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;   [t1].[OrderDate], [t1].[RequiredDate], [t1].[ShippedDate], &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;   [t1].[ShipVia], [t1].[Freight], [t1].[ShipName], [t1].[ShipAddress],&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;   [t1].[ShipCity], [t1].[ShipRegion], [t1].[ShipPostalCode], [t1].[ShipCountry]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="kwrd"&gt;FROM&lt;/span&gt; (&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;SELECT&lt;/span&gt; ROW_NUMBER() &lt;span class="kwrd"&gt;OVER&lt;/span&gt; (&lt;span class="kwrd"&gt;ORDER&lt;/span&gt; &lt;span class="kwrd"&gt;BY&lt;/span&gt; [t0].[OrderDate]) &lt;span class="kwrd"&gt;AS&lt;/span&gt; [ROW_NUMBER], &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;        [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;        [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;        [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;        [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;    &lt;span class="kwrd"&gt;FROM&lt;/span&gt; [Orders] &lt;span class="kwrd"&gt;AS&lt;/span&gt; [t0]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;    ) &lt;span class="kwrd"&gt;AS&lt;/span&gt; [t1]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="kwrd"&gt;WHERE&lt;/span&gt; [t1].[ROW_NUMBER] &amp;gt; @p0&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;ORDER&lt;/span&gt; &lt;span class="kwrd"&gt;BY&lt;/span&gt; [t1].[ROW_NUMBER]&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;You can also achieve the same result with a query like: &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; * &lt;span class="kwrd"&gt;from&lt;/span&gt; Orders&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="kwrd"&gt;WHERE&lt;/span&gt; OrderId &lt;span class="kwrd"&gt;not&lt;/span&gt; &lt;span class="kwrd"&gt;in&lt;/span&gt; &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;   (&lt;span class="kwrd"&gt;select&lt;/span&gt; &lt;span class="kwrd"&gt;top&lt;/span&gt; 5 OrderID &lt;span class="kwrd"&gt;from&lt;/span&gt; orders &lt;span class="kwrd"&gt;ORDER&lt;/span&gt; &lt;span class="kwrd"&gt;BY&lt;/span&gt; OrderDate)&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Take:&lt;/strong&gt; Takes top N – IOW typical TOP N query operator.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;Orders.OrderBy(o =&amp;gt; o.OrderDate).Take(5)&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&amp;#160;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;T-SQL counterpart:&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; &lt;span class="kwrd"&gt;TOP&lt;/span&gt; 5 * &lt;span class="kwrd"&gt;FROM&lt;/span&gt; [Orders] &lt;span class="kwrd"&gt;order&lt;/span&gt; &lt;span class="kwrd"&gt;by&lt;/span&gt; OrderDate&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&amp;#160;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;Both Skip and Take have another version which is not supported by L2S:&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&amp;#160;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;strong&gt;SkipWhile:&lt;/strong&gt; Skips over elements as long as a condition is true. It has 2 overloads:&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;  &lt;li&gt;&lt;br /&gt;    &lt;div class="csharpcode"&gt;First overload skips while a condition is true.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;    &lt;div class="csharpcode"&gt;&lt;br /&gt;      &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;int&lt;/span&gt;[] numbers = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17};&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;      &lt;pre&gt;var some = numbers.SkipWhile( n =&amp;gt; n &amp;lt; 5 );&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;      &lt;pre class="alt"&gt;some.Dump(&lt;span class="str"&gt;&amp;quot;Skipped while number is less than 5&amp;quot;&lt;/span&gt;);&lt;/pre&gt;&lt;br /&gt;    &lt;/div&gt;&lt;br /&gt;    &lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;br /&gt;    &lt;div class="csharpcode"&gt;Second overload skips while a condition is true and also gets an index parameter as input.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;    &lt;div class="csharpcode"&gt;&lt;br /&gt;      &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;int&lt;/span&gt;[] numbers = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17};&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;      &lt;pre&gt;var some = numbers.SkipWhile( (n, index) =&amp;gt; index &amp;lt; 5);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;      &lt;pre class="alt"&gt;some.Dump(&lt;span class="str"&gt;&amp;quot;Skipped while index position is less than 5&amp;quot;&lt;/span&gt;);&lt;/pre&gt;&lt;br /&gt;    &lt;/div&gt;&lt;br /&gt;    &lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;TakeWhile: Takes elements as long as the condition is true. Similar to SkipWhile it has same 2 overloads.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&amp;#160;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;int&lt;/span&gt;[] numbers = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17};&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;var some = numbers.TakeWhile( n =&amp;gt; n &amp;lt; 5);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;some.Dump(&lt;span class="str"&gt;&amp;quot;Take while number is less than 5&amp;quot;&lt;/span&gt;);&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;int&lt;/span&gt;[] numbers = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17};&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;var some = numbers.TakeWhile( (n, index) =&amp;gt; index &amp;lt; 5);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;some.Dump(&lt;span class="str"&gt;&amp;quot;Take while index position is less than 5&amp;quot;&lt;/span&gt;);&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;font color="#000000" size="2" face="Consolas"&gt;&lt;/font&gt;&amp;#160;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;font color="#000000" size="2"&gt;You can combine or in other words chain these operators. Here is a sample (and already we did one before using OrderBy() above):&lt;/font&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;Orders&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt; .OrderBy( o =&amp;gt; o.CustomerID)&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt; .Where( o =&amp;gt; o.Customer.Country == &lt;span class="str"&gt;&amp;quot;USA&amp;quot;&lt;/span&gt; )&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt; .Skip(5)&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt; .Take(3)&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;font color="#000000" size="2"&gt;Note that using Skip() and Take() combination you can easily create a &amp;quot;paging query&amp;quot; for a web application. For example this gets the records 21-30 (Page 3 where pages are 10 records high) in a particular order of a paged query:&lt;/font&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;Customers.OrderBy(c =&amp;gt; c.CustomerID).Skip( 2*10 ).Take(10)&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-7861549233019180620?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/7861549233019180620/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=7861549233019180620&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/7861549233019180620'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/7861549233019180620'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/05/linq-query-operators-part-i.html' title='LINQ Query Operators – Part I'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-1033091388049729283</id><published>2009-04-23T03:54:00.002+03:00</published><updated>2009-04-23T03:57:41.203+03:00</updated><title type='text'>A few Linq resources</title><content type='html'>&lt;p&gt;It has been a long time since I posted my last entry. Sorry about that. I still don't have time for long posts but I thought I could at least provide some pointers to resources, samples, tools and such. &lt;/p&gt;  &lt;p&gt;Here is a new book that is published recently:&lt;/p&gt;  &lt;p&gt;Essential LINQ    &lt;br /&gt;by Charlie Calvert and Dinesh Kulkarni     &lt;br /&gt;Publisher: Addison-Wesley Professional     &lt;br /&gt;Pub Date: March 12, 2009     &lt;br /&gt;Print ISBN-10: 0-321-56416-2     &lt;br /&gt;Print ISBN-13: 978-0-321-56416-0     &lt;br /&gt;Web ISBN-10: 0-321-60475-X     &lt;br /&gt;Web ISBN-13: 978-0-321-60475-0     &lt;br /&gt;Pages: 600&lt;/p&gt;  &lt;p&gt;I haven't read this book myself and learned about it today just by accident while testing a Linq sample delivered by Microsoft:)&lt;/p&gt;  &lt;p&gt;Be sure to check samples in LinqPad and if you have Visual Studio 2008 check the samples delivered with VS located at:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;C:\Program Files\Microsoft Visual Studio 9.0\Samples\1033\CSharpSamples\LinqSamples&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Samples do not exist in Express version as far as I know but you could get the online version which is updated and maybe more recent then the one you have from:&lt;/p&gt;  &lt;p&gt;&lt;a title="http://code.msdn.microsoft.com/csharpsamples" href="http://code.msdn.microsoft.com/csharpsamples"&gt;http://code.msdn.microsoft.com/csharpsamples&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Some more Linq flavors (not released yet – alpha,beta):&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Parallel Linq &lt;/li&gt;    &lt;li&gt;Linq to SharePoint &lt;/li&gt;    &lt;li&gt;Linq to Active Directory &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;And finally I want to include a short reply to a question asked by a friend:&lt;/p&gt;  &lt;p&gt;Question: Is Linq about querying data or can we use it to update the data too?&lt;/p&gt;  &lt;p&gt;Answer: As I remember I told this before, Linq might be a misnomer and it is also used for updating the data where applicable. How we do it depends on which flavor we are using, but the key point is that while doing that we use an &amp;quot;object oriented&amp;quot; approach. &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-1033091388049729283?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/1033091388049729283/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=1033091388049729283&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/1033091388049729283'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/1033091388049729283'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/04/few-linq-resources.html' title='A few Linq resources'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-4569089134458913971</id><published>2009-04-01T04:44:00.001+03:00</published><updated>2009-04-01T04:44:54.221+03:00</updated><title type='text'>Query keywords – join, let</title><content type='html'>&lt;p&gt;&lt;strong&gt;join&lt;/strong&gt;: Similar to SQL join, the join keyword is used to join sets. One thing you should note that, if there is a direct relationship in the object model a join is not needed. This behavior differs from SQL. Consider the infamous Northwind database. In SQL server (and in VFP northwind.dbc sample database) there are relations between the tables defined.&amp;#160; You can't simply create joins without specifying a relation in your SQL:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;select * from Customers, Orders &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;would be a newbie's error (most of the time) in an attempt to join two tables and expect them to be joined using CustomerId. End result would be a Cartesian join matching each Customer with every Order in Orders set. With Linq you can make the same mistake but it is harder to do:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;from c in Customers      &lt;br /&gt;from o in Orders       &lt;br /&gt;select new { c.CustomerID, o.OrderID }&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;With the above SQL our intention would likely be an equijoin:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;select &lt;font color="#000000"&gt;Customers.CustomerID, Orders.OrderID&lt;/font&gt; from Customers, Orders where Customers.CustomerID = Orders.CustomerID &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;select &lt;font color="#000000"&gt;Customers.CustomerID, Orders.OrderID&lt;/font&gt; from Customers inner join Orders on Customers.CustomerID = Orders.CustomerID &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;select &lt;font color="#000000"&gt;Customers.CustomerID, Orders.OrderID&lt;/font&gt; from Customers left join Orders on Customers.CustomerID = Orders.CustomerID&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;First two are typical inner join queries and 3rd left outer join. In Linq writing such queries is easy. You could write the inner join like this one:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;from c in Customers      &lt;br /&gt;from o in c.Orders       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; select new { c.CustomerID, o.OrderID }&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;and left outer join like this one:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;from c in Customers&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; select new { c.CustomerID, OrderID = c.Orders.Select( o =&amp;gt; o.OrderID ) }&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;While you could use &amp;quot;join&amp;quot; in these, join in Linq is for creating joins on properties that doesn't already define a relationship in object model (it is easy to forget we are doing a query against objects and object models). Within a database it is unlikely to make join queries that are not already defined with relationships but you may never know what you need. And you wouldn't always use Linq against collections that come from database based or some other type of well defined object model. We will however see &amp;quot;join&amp;quot; here with already well defined relationships just for the sake of your familiarity with those table and key field names. It makes it easier to guess the result without actually seeing it then.&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;from c in Customers      &lt;br /&gt;join o in Orders on c.CustomerID equals o.CustomerID       &lt;br /&gt;select new {c.CustomerID, o.OrderID }&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;I should also note that using join on primary – foreign key properties causes an &amp;quot;inner join&amp;quot; to be generated. There is also another type of join called &amp;quot;&lt;strong&gt;Group Join&lt;/strong&gt;&amp;quot;. You can create group join using &amp;quot;join&amp;quot; and &amp;quot;into&amp;quot; clauses (keywords) together:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;from c in Customers      &lt;br /&gt;join o in Orders on c.CustomerID equals o.CustomerID into orderGroup       &lt;br /&gt;select new {c.CustomerID, orderGroup }&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;Or if we remember that we are working with objects could write it like this as well:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;from c in Customers      &lt;br /&gt;join o in Orders on c equals o.Customer into orderGroup       &lt;br /&gt;select new {c.CustomerID, orderGroup }&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;But also there is one another and maybe easier to write and understand variation of the same query (and both generate the same left outer join) – provided there is a relationship that we can use in object model (you may call it &amp;quot;relationship&amp;quot; or &amp;quot;navigational property&amp;quot;):&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;from c in Customers select new {c.CustomerID, c.Orders}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;let&lt;/strong&gt;: Let is a cool keyword&amp;#160; that allows you to store inline expressions. Those who use SQL server 2005 and later know that how much CTE (Common Table Expression - with {…} as ) is welcomed easing the writing of queries and increasing the performance as well. Let keyword is not CTE of course but looks very similar. Using it you assign a variable a complete expression inline creating a &amp;quot;sub-expression&amp;quot;. That sub-expression is evaluated only once and can be used in operations within the outer expression. Now let's see how let lets you &amp;lt; g &amp;gt; write more succinct expressions. I will start with an exact copy from LinqPad's 5 minutes samples:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;from p in Products      &lt;br /&gt;let spanishOrders = p.OrderDetails.Where (o =&amp;gt; o.Order.ShipCountry == &amp;quot;Spain&amp;quot;)       &lt;br /&gt;where spanishOrders.Any()       &lt;br /&gt;orderby p.ProductName       &lt;br /&gt;select new       &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; p.ProductName,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; p.Category.CategoryName,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Orders = spanishOrders.Count(),&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; TotalValue = spanishOrders.Sum (o =&amp;gt; o.UnitPrice * o.Quantity)       &lt;br /&gt;}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;Here &lt;strong&gt;spanishOrders&lt;/strong&gt; is an inline variable (well not very true to call it variable, purists should forgive me) whose value is assigned from&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;p.OrderDetails.Where (o =&amp;gt; o.Order.ShipCountry == &amp;quot;Spain&amp;quot;)&lt;/font&gt;&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;expression. This expression is the same as this one written in comprehension syntax:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;from o in p.OrderDetails where o.Order.ShipCountry == &amp;quot;Spain&amp;quot; select o&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;(and you could replace it with this one enclosing it in parentheses). It is later used in 3 places:&lt;/p&gt;  &lt;p&gt;where &lt;strong&gt;spanishOrders&lt;/strong&gt;.Any()     &lt;br /&gt;orderby p.ProductName     &lt;br /&gt;select new     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; p.ProductName,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; p.Category.CategoryName,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Orders = &lt;strong&gt;spanishOrders&lt;/strong&gt;.Count(),&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; TotalValue = &lt;strong&gt;spanishOrders&lt;/strong&gt;.Sum (o =&amp;gt; o.UnitPrice * o.Quantity)     &lt;br /&gt;}&lt;/p&gt;  &lt;p&gt;I think you can see it without any further explanation. It is acting like an extraordinary SQL subquery (but it doesn't need to be something like a subquery, any expression would do and if that expression is returning something queryable you can query it). Here is another sample (this sample is from .Net documentation, you can see the original &lt;a href="http://msdn.microsoft.com/en-us/library/bb383976.aspx" target="_blank"&gt;here&lt;/a&gt;):&lt;/p&gt;  &lt;p&gt;string[] strings =    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;A penny saved is a penny earned.&amp;quot;,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;The early bird catches the worm.&amp;quot;,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;The pen is mightier than the sword.&amp;quot;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }; &lt;/p&gt;  &lt;p&gt;// Split the sentence into an array of words    &lt;br /&gt;// and select those whose first letter is a vowel.     &lt;br /&gt;var earlyBirdQuery =     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; from sentence in strings     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; let words = sentence.Split(' ')     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; from word in words     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; let w = word.ToLower()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; where w[0] == 'a' || w[0] == 'e'     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; || w[0] == 'i' || w[0] == 'o'     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; || w[0] == 'u'     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; select word; &lt;/p&gt;  &lt;p&gt;earlyBirdQuery.Dump(); &lt;/p&gt;  &lt;p&gt;There are two let keywords here. First one gets each &lt;strong&gt;sentence&lt;/strong&gt; and stores the result of &lt;strong&gt;Split(' ')&lt;/strong&gt; function to &lt;strong&gt;words&lt;/strong&gt;. &amp;quot;words&amp;quot; is another enumerable ( words of sentences in string ) and it is followed by another query ( from word in words ). For each word second &amp;quot;let&amp;quot; stores the result of word.Lower() to &amp;quot;w&amp;quot;. &amp;quot;where&amp;quot; clause use that w multiple times. Without &amp;quot;let&amp;quot; word.Lower() itself would be called multiple times ( where w[0] == 'a' || w[0] == 'e' … w[0] == 'u' ). &lt;/p&gt;  &lt;p&gt;Here is another sample which I wrote as a reply on MSDN C# forum. The question was (as I remember it):&lt;/p&gt;  &lt;p&gt;There is tickdata like this one:&lt;/p&gt;  &lt;p&gt;create table tickdata (timed datetime, price money, quantity int)    &lt;br /&gt;insert into tickData values ('06:01',100,20)     &lt;br /&gt;insert into tickData values ('06:02',90,30)     &lt;br /&gt;insert into tickData values ('06:03',129,10)     &lt;br /&gt;insert into tickData values ('06:11',112,8)     &lt;br /&gt;insert into tickData values ('06:12',150,60)     &lt;br /&gt;insert into tickData values ('06:20',110,10)     &lt;br /&gt;insert into tickData values ('06:23',120,5)     &lt;br /&gt;insert into tickData values ('06:24:59',130,15)     &lt;br /&gt;insert into tickData values ('06:25',140,35) &lt;/p&gt;  &lt;p&gt;From this data generate stock trade like data that looks like:&lt;/p&gt;  &lt;table border="1" cellspacing="0" cellpadding="2"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;th&gt;period&lt;/th&gt;        &lt;th&gt;open&lt;/th&gt;        &lt;th&gt;low&lt;/th&gt;        &lt;th&gt;high&lt;/th&gt;        &lt;th&gt;close&lt;/th&gt;        &lt;th&gt;volume&lt;/th&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;06:00 - 06:05&lt;/td&gt;        &lt;td&gt;100.0000&lt;/td&gt;        &lt;td&gt;90.0000&lt;/td&gt;        &lt;td&gt;129.0000&lt;/td&gt;        &lt;td&gt;129.0000&lt;/td&gt;        &lt;td&gt;60&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;06:10 - 06:15&lt;/td&gt;        &lt;td&gt;112.0000&lt;/td&gt;        &lt;td&gt;112.0000&lt;/td&gt;        &lt;td&gt;150.0000&lt;/td&gt;        &lt;td&gt;150.0000&lt;/td&gt;        &lt;td&gt;68&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;06:20 - 06:25&lt;/td&gt;        &lt;td&gt;110.0000&lt;/td&gt;        &lt;td&gt;110.0000&lt;/td&gt;        &lt;td&gt;130.0000&lt;/td&gt;        &lt;td&gt;130.0000&lt;/td&gt;        &lt;td&gt;30&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;06:25 - 06:30&lt;/td&gt;        &lt;td&gt;140.0000&lt;/td&gt;        &lt;td&gt;140.0000&lt;/td&gt;        &lt;td&gt;140.0000&lt;/td&gt;        &lt;td&gt;140.0000&lt;/td&gt;        &lt;td&gt;35&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;where period is defined as 5 minute intervals starting from the first available tickdata time and shows for each period with data. This was the Linq solution I came up with (besides &amp;quot;let&amp;quot; keyword there is grouping and query methods):&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;from myData in Tickdata.OrderBy( t =&amp;gt; t.Timed ).AsEnumerable()      &lt;br /&gt;&amp;#160; group myData by ((int)myData.Timed.Value.TimeOfDay.TotalMinutes) / 5 into barData       &lt;br /&gt;&amp;#160; let bStart = TimeSpan.FromMinutes( barData.Key * 5 ).ToString()       &lt;br /&gt;&amp;#160; let bEnd&amp;#160;&amp;#160; = TimeSpan.FromMinutes( (barData.Key+1) * 5 ).ToString()       &lt;br /&gt;&amp;#160; select new       &lt;br /&gt;&amp;#160; { period = bStart.Substring(0,5) + &amp;quot; - &amp;quot; + bEnd.Substring(0,5),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; open&amp;#160;&amp;#160; = barData.FirstOrDefault().Price,&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; low&amp;#160;&amp;#160;&amp;#160; = barData.Min( bd =&amp;gt; bd.Price ),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; high&amp;#160;&amp;#160; = barData.Max( bd =&amp;gt; bd.Price ),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; close&amp;#160; = barData.LastOrDefault().Price,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; volume = barData.Sum( bd =&amp;gt; bd.Quantity ) }&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;This concludes our query keywords series. Other keywords are conceptual ones used to complete syntax (in, on, equals, by, ascending, descending).&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-4569089134458913971?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/4569089134458913971/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=4569089134458913971&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/4569089134458913971'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/4569089134458913971'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/04/query-keywords-join-let.html' title='Query keywords – join, let'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-8427153828220409438</id><published>2009-03-23T01:33:00.001+02:00</published><updated>2009-03-23T01:33:14.269+02:00</updated><title type='text'>Query keywords – orderby</title><content type='html'>&lt;p&gt;&lt;strong&gt;orderby&lt;/strong&gt;: Is used for sorting the records. In comprehension syntax it is just like we use it in SQL. i.e:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;from c in Customers      &lt;br /&gt;orderby c.Country, c.CompanyName descending       &lt;br /&gt;select c&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Here we are ordering by Country (default ascending) and then by CompanyName in descending order.&lt;/p&gt;  &lt;p&gt;In method syntax there are 4 methods:&lt;/p&gt;  &lt;p&gt;OrderBy()    &lt;br /&gt;OrderByDescending()     &lt;br /&gt;ThenBy()     &lt;br /&gt;ThenByDescending()&lt;/p&gt;  &lt;p&gt;Using method syntax above sample would be written as:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;Customers      &lt;br /&gt;&amp;#160; .OrderBy( c =&amp;gt; c.Country )       &lt;br /&gt;&amp;#160; .ThenByDescending( c =&amp;gt; c.CompanyName )       &lt;br /&gt;&amp;#160; .Select( c =&amp;gt; c)&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;All 4 methods support a custom comparison method as a second parameter (for example you could create a method that instead of widely known alphabetical sort, sorts the strings based on a custom ordering – &amp;quot;10&amp;quot; comes after &amp;quot;2&amp;quot;).&lt;/p&gt;  &lt;p&gt;Here is a sample modified from LinqPad's OrderBy sample:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;string[] strings = { &amp;quot;Tom&amp;quot;, &amp;quot;Çetin&amp;quot;,      &lt;br /&gt;&amp;#160;&amp;#160; &amp;quot;I come before i&amp;quot;, &amp;quot;Can&amp;quot;, &amp;quot;in&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160; &amp;quot;Dick&amp;quot;, &amp;quot;Harry&amp;quot;, &amp;quot;Mary&amp;quot;, &amp;quot;Jay&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160; &amp;quot;ş is between s and t&amp;quot;, &amp;quot;smith&amp;quot;}; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;strings.OrderBy (n =&amp;gt; n, StringComparer.Create( new System.Globalization.CultureInfo(&amp;quot;tr-TR&amp;quot;), true))      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; .Dump (&amp;quot;Case insensitive ordering based on Turkish alphabet&amp;quot;);&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;This second parameter is not supported in Linq to SQL.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-8427153828220409438?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/8427153828220409438/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=8427153828220409438&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/8427153828220409438'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/8427153828220409438'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/03/query-keywords-orderby.html' title='Query keywords – orderby'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-1879795965964834737</id><published>2009-03-17T01:57:00.001+02:00</published><updated>2009-03-17T01:57:22.388+02:00</updated><title type='text'>Query keywords – group and into</title><content type='html'>&lt;p&gt;&lt;strong&gt;group (… by ):&lt;/strong&gt; Is used for grouping the results based on a key. The key can be a plain or composite key. In Linq, the result of grouping is a List of Lists. You can think of it as data grouping in a report rather than a flat rows*columns representation of a table. The result is&amp;#160; ( A Key per group, Matching Items per key ) collection – officially IGrouping ( TKey, TElement ). Another feature of group is that it is a keyword that can be used to end a query ( a query should end either with a select or group ). &lt;/p&gt;  &lt;p&gt;A simple grouping would be to group Northwind Customers by their country values:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;from c in Customers group c by c.Country&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;You can &amp;quot;group&amp;quot;, &amp;quot;select&amp;quot; or &amp;quot;join&amp;quot; into an intermediary result using the keyword &lt;strong&gt;&amp;quot;into&amp;quot;&lt;/strong&gt; and continue the query on that 'sub query'. It is like doing a sub query in SQL. &amp;quot;Key&amp;quot; is a property that is generated automatically and holds the group's key. i.e.:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;from c in Customers      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; group c by c.Country into countries       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; select new       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Country = countries.Key,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Customers = from cus in countries&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;orderby cus.City, cus.CustomerID      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; select new { cus.CustomerID, cus.City }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Here what we do is first we group the Customers by their country and 'alias' that 'sub query' as &amp;quot;countries&amp;quot;. From that point on identifier &amp;quot;c&amp;quot; is not available to us (it is similar to SQL in that matter too). We continue with &amp;quot;countries&amp;quot; which is now a ( Country as key, Matching Rows ) collection.&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;You can also group a group but IMHO it is a hard to understand syntax:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;from c in Customers.Select( c =&amp;gt; new {c.CustomerID, c.Region, c.City, c.Country} )      &lt;br /&gt;&amp;#160;&amp;#160; group c by c.Country[0] into g1       &lt;br /&gt;&amp;#160;&amp;#160; from cc in ( from c in g1 group c by c.Country )       &lt;br /&gt;&amp;#160;&amp;#160; group cc by g1.Key&lt;/font&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-1879795965964834737?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/1879795965964834737/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=1879795965964834737&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/1879795965964834737'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/1879795965964834737'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/03/query-keywords-group-and-into.html' title='Query keywords – group and into'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-1023206749600718995</id><published>2009-03-11T04:48:00.001+02:00</published><updated>2009-03-11T04:48:58.809+02:00</updated><title type='text'>Query keywords - select</title><content type='html'>&lt;p&gt;Before starting &amp;quot;select&amp;quot; I would like to list where to find LinqPad, SQL express, sample Northwind database, VC# express. To follow you need LinqPad (VS or VC# express IDE would do too, but I repeat LinqPad is a cool utility that worth the 3Mb download).&lt;/p&gt;  &lt;p&gt;Linqpad download: &lt;a href="http://www.LinqPad.Net"&gt;http://www.LinqPad.Net&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;SQL express version download: &lt;a title="http://www.microsoft.com/express/sql/default.aspx" href="http://www.microsoft.com/express/sql/default.aspx"&gt;http://www.microsoft.com/express/sql/default.aspx&lt;/a&gt; (you may also download cool SQL CE for some samples in the future – or to use from VFP)&lt;/p&gt;  &lt;p&gt;VC# (or any one of the express): &lt;a title="http://www.microsoft.com/express/product/" href="http://www.microsoft.com/express/product/"&gt;http://www.microsoft.com/express/product/&lt;/a&gt; (If you check specific product pages there are tons of extras but don't get lost in them:)&lt;/p&gt;  &lt;p&gt;Northwind database: &lt;a title="http://www.microsoft.com/downloads/details.aspx?FamilyID=06616212-0356-46A0-8DA2-EEBC53A68034&amp;amp;displaylang=en" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=06616212-0356-46A0-8DA2-EEBC53A68034&amp;amp;displaylang=en"&gt;http://www.microsoft.com/downloads/details.aspx?FamilyID=06616212-0356-46A0-8DA2-EEBC53A68034&amp;amp;displaylang=en&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;after downloading run SQL2000SampleDb.msi. It creates a new folder named &amp;quot;SQL Server 2000 Sample Databases&amp;quot;, in it you would find both the sql scripts and database files (Northwind and Pubs). Either attach the database or run the sql script. i.e: In command prompt you can go to that folder and execute:&lt;/p&gt;  &lt;p&gt;&lt;font size="2" face="Courier New"&gt;sqlcmd -S .\sqlexpress -i instnwnd.sql –E&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2" face="Courier New"&gt;( –S stands for SQL server instance which by default is .\SQLExpress for express edition, –i is input script file, and –E parameter tells that we are using windows authentication). &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;(you can also try this: &lt;a title="http://msdn.microsoft.com/en-us/library/8b6y4c7s(VS.80).aspx" href="http://msdn.microsoft.com/en-us/library/8b6y4c7s(VS.80).aspx"&gt;http://msdn.microsoft.com/en-us/library/8b6y4c7s(VS.80).aspx&lt;/a&gt; )&lt;/p&gt;  &lt;p&gt;OK now let's continue.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;select:&lt;/strong&gt; is the keyword for selecting of course:) Linq however can do a select that can do more than flat retrieval. It can transform the data it retrieves (projection, shaping data) in a variety of ways.&amp;#160; With SQL select the result may be transformed. i.e:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;select employeeID, rtrim(LastName) + ', ' + rtrim(FirstName) as Fullname from employees&lt;/font&gt;&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;Fullname is a transformation of data but still we have a flat result made up of rows and columns. However with Linq we can do much more complex shaping on the data retrieved.&lt;/p&gt;  &lt;p&gt;select clause too also&amp;#160; have method counterpart Select(). Select() method just like Where() have 2 overloads. Second overload, like Where, has an index parameter and not supported by Linq to entities (you can find a list of supported and non supported methods for Linq to entities &lt;a href="http://msdn.microsoft.com/en-us/library/bb738550.aspx" target="_blank"&gt;here.&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Do you always need to use select clause or Select() method? No, you don't have to. Using query syntax you need to end the expression with a 'select' or 'group' clause.&amp;#160; You can have do these:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;var customers = Customers; &lt;font color="#008000"&gt;// in LinqPad Northwind set as the database you may try just typing: Customers [F5]&lt;/font&gt;&amp;#160; &lt;br /&gt;var usaCustomers =&amp;#160; Customers.Where( c =&amp;gt; c.Country == &amp;quot;USA&amp;quot; );&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;You may think of these variations like:&lt;/p&gt;  &lt;p&gt;use customers&lt;/p&gt;  &lt;p&gt;and&lt;/p&gt;  &lt;p&gt;use customers    &lt;br /&gt;scan for Country = &amp;quot;USA&amp;quot;&lt;/p&gt;  &lt;p&gt;We have done these before like:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;from c in Customers select c&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;from c in Customers where c.Country == &amp;quot;USA&amp;quot; select c&lt;/font&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;These are &amp;quot;expressions&amp;quot; you can directly type in LinqPad and hit F5 to get results. &lt;/p&gt;  &lt;p&gt;You can shape the result in a variety ways and to create new shapes you use &amp;quot;&lt;strong&gt;new { … }&lt;/strong&gt;&amp;quot; as we saw earlier in creating anonymous types:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;from c in Customers select new { c.CompanyName, c.ContactName }&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;from c in Customers select new { MyCompany = c.CompanyName, c.ContactName }&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;are simple transformations and correspond to:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="courier n"&gt;select c.CompanyName, c.ContactName from Customers&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="courier n"&gt;select c.CompanyName as MyCompany, c.ContactName from Customers&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;in SQL. Now more interesting selects – try these in LinqPad:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;from c in Customers      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; where c.Country == &amp;quot;USA&amp;quot; &amp;amp;&amp;amp; c.Region == &amp;quot;WA&amp;quot;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; select c.Orders&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;from c in Customers      &lt;br /&gt;from o in c.Orders       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; where c.Country == &amp;quot;USA&amp;quot; &amp;amp;&amp;amp; c.Region == &amp;quot;WA&amp;quot;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; select new {CustomerInfo = c, OrderInfo = o}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;you can have many variations, you can embed little lambda expression to transform further or utilize Linq to XML to generate XML results. i.e:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;from c in Customers      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; select new XElement (&amp;quot;customer&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new XAttribute (&amp;quot;id&amp;quot;, c.CustomerID),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new XAttribute (&amp;quot;company&amp;quot;, c.CompanyName),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new XElement (&amp;quot;contact&amp;quot;, c.ContactName),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new XElement (&amp;quot;country&amp;quot;, c.Country),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new XComment (&amp;quot;Total Orders:&amp;quot;+c.Orders.Count().ToString()))&lt;/font&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-1023206749600718995?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/1023206749600718995/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=1023206749600718995&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/1023206749600718995'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/1023206749600718995'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/03/query-keywords-select.html' title='Query keywords - select'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-963242990221810924</id><published>2009-03-08T22:01:00.003+02:00</published><updated>2009-03-08T22:25:07.714+02:00</updated><title type='text'>Query keywords - where</title><content type='html'>&lt;p class="MsoNormal"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;where:&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt; Just like in SQL “where” is used to filter the query results. Though not supported by all flavors (like Linq to SQL doesn’t support) if method syntax is used, then you can also filter on based on position in collection (i.e: if you are filtering an array you can say to choose from each Nth element where N is even – see the sample below). &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;Samples assume Northwind sample database is used for the ones against SQL data – to do that with LinqPad: &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;Click “Add Connection”, point to your SQL server instance that have the “Northwind” database. If database is not already selected in query window, click “Use Northwind” link displayed in window at right as seen in the following screen shot: &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;a href="http://1.bp.blogspot.com/_rkddjaBL1xc/SbQkeBbWlpI/AAAAAAAAACg/uVXG7LcLua0/s1600-h/linqPad_SetDatabase.JPG"&gt;&lt;img style="MARGIN: 0px 10px 10px 0px; WIDTH: 644px; FLOAT: left; HEIGHT: 274px; CURSOR: hand" id="BLOGGER_PHOTO_ID_5310909958774494866" border="0" alt="" src="http://1.bp.blogspot.com/_rkddjaBL1xc/SbQkeBbWlpI/AAAAAAAAACg/uVXG7LcLua0/s320/linqPad_SetDatabase.JPG" width="644" height="257" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;With this setup you can simply type the codes below and press F5 to see results. &lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;table border="1" cellspacing="0" cellpadding="0" width="100%" &gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td valign="top" width="50%" &gt;&lt;br /&gt;&lt;p&gt;&lt;span style="FONT-FAMILY: Courier New;font-size:10;color:blue;"   &gt;from&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c in Customers&lt;br /&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;where&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c.Country == &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"USA"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;select&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;VB version: &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;from&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c in Customers _&lt;br /&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;where&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c.Country = &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:maroon;"   &gt;"USA"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; _&lt;br /&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;select&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;Method syntax (C#): &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;from&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c in Customers.Where( c =&amp;gt; c.Country == &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"USA"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; )&lt;br /&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;select&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;Method syntax (C#) – same thing typed another way: &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;from&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c in Customers&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;.Where( c =&amp;gt; c.Country == &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"USA"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; )&lt;br /&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;select&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c&lt;/span&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt; &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;Method syntax (C#) – same thing typed another way: &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;from&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c in Customers&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;.Where( c =&amp;gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;c.Country == &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"USA"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; )&lt;br /&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;select&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;Method syntax (VB): &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;from&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c in Customers _&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;.Where( &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;Function&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;(c) c.Country = &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:maroon;"   &gt;"USA"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; ) _&lt;br /&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;select&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td style="BORDER-BOTTOM: black 1pt solid; PADDING-BOTTOM: 0cm; PADDING-LEFT: 5.4pt; WIDTH: 290.6pt; PADDING-RIGHT: 5.4pt; BORDER-LEFT-COLOR: black; BORDER-TOP: black 1pt solid; BORDER-LEFT-WIDTH: 1pt; BORDER-RIGHT: black 1pt solid; PADDING-TOP: 0cm; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-border-left-alt: solid black .5ptcolor:text1;" valign="top" width="452" &gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;Simple where filtering on Customers whose Country is USA &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;&lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;Note that in all syntaxes that is a single statement (single command). You can type it in different ways using continuation (in C# you don’t need any continuation line marker – where in VB you use underscore and VFP semicolon). &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;VB method syntax is a little weird and hard to understand for me, I wouldn’t show VB method syntax in other samples. It should however be easy to understand for VB developers. &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;Within method call the variable “c” is local to Where() method and could be something else. i.e: &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;from&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c in Customers&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;.Where( cus =&amp;gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;cus.Country == &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"USA"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; )&lt;br /&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;select&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;Using method syntax we would see it is easier (it is not clear here why it is) but keeping that to later. Here it is just another way of writing the same thing. &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr style="HEIGHT: 122.25pt; mso-yfti-irow: 1"&gt;&lt;br /&gt;&lt;td style="BORDER-BOTTOM: black 1pt solid; BORDER-LEFT: black 1pt solid; PADDING-BOTTOM: 0cm; BORDER-TOP-COLOR: black; PADDING-LEFT: 5.4pt; WIDTH: 325.65pt; PADDING-RIGHT: 5.4pt; BORDER-TOP-WIDTH: 1pt; HEIGHT: 122.25pt; BORDER-RIGHT: black 1pt solid; PADDING-TOP: 0cm; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-border-top-alt: solid black .5ptcolor:text1;" valign="top" width="500" &gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;from&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c in Customers&lt;br /&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;where&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c.Country == &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"USA"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; &amp;amp;&amp;amp; c.Orders.Count &amp;lt; &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#c81efa;"   &gt;5&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;select&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c&lt;/span&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt; &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;VB version: &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;from&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c in Customers _&lt;br /&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;where&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c.Country = &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:maroon;"   &gt;"USA"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;and&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c.Orders.Count &amp;lt; &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:purple;"   &gt;5&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; _&lt;br /&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;select&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;Method syntax (C#): &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;from&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c in Customers&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;.Where( c =&amp;gt; c.Country == &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"USA"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; &amp;amp;&amp;amp;&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;c.Orders.Count &amp;lt; &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#c81efa;"   &gt;5&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; )&lt;br /&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;select&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c&lt;/span&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt; &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td style="BORDER-BOTTOM: black 1pt solid; PADDING-BOTTOM: 0cm; PADDING-LEFT: 5.4pt; WIDTH: 290.6pt; PADDING-RIGHT: 5.4pt; BORDER-TOP-STYLE: none; HEIGHT: 122.25pt; BORDER-LEFT-STYLE: none; BORDER-RIGHT: black 1pt solid; PADDING-TOP: 0cm; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; mso-border-bottom-themecolor: text1color:text1;" valign="top" width="452" &gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;Adding additional criteria. Note the navigational access to Orders of Customer simply using c.Orders and then Count property on &lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;customer’s Orders &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr style="HEIGHT: 173.25pt; mso-yfti-irow: 2; mso-yfti-lastrow: yes"&gt;&lt;br /&gt;&lt;td style="BORDER-BOTTOM: black 1pt solid; BORDER-LEFT: black 1pt solid; PADDING-BOTTOM: 0cm; BORDER-TOP-COLOR: black; PADDING-LEFT: 5.4pt; WIDTH: 325.65pt; PADDING-RIGHT: 5.4pt; BORDER-TOP-WIDTH: 1pt; HEIGHT: 173.25pt; BORDER-RIGHT: black 1pt solid; PADDING-TOP: 0cm; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-border-top-alt: solid black .5ptcolor:text1;" valign="top" width="500" &gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;from&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c in Customers&lt;br /&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;where&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c.Country == &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"USA"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; &amp;amp;&amp;amp;&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;c.Orders.First().OrderDate &amp;lt; &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;new&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; DateTime(&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#c81efa;"   &gt;1997&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;,&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#c81efa;"   &gt;1&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;,&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#c81efa;"   &gt;1&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;select&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c&lt;/span&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt; &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;VB version: &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;from&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c in Customers _&lt;br /&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;where&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c.Country = &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:maroon;"   &gt;"USA"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;and&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; _&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;c.Orders.First().OrderDate &amp;lt; &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;new&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; DateTime(&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:purple;"   &gt;1997&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;,&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:purple;"   &gt;1&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;,&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:purple;"   &gt;1&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;) _&lt;br /&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;select&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;Method syntax (C#): &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;from&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c in Customers&lt;br /&gt;.Where( c =&amp;gt; c.Country == &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"USA"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; &amp;amp;&amp;amp;&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;c.Orders.First().OrderDate &amp;lt;&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;new&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; DateTime(&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#c81efa;"   &gt;1997&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;,&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#c81efa;"   &gt;1&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;,&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#c81efa;"   &gt;1&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;) )&lt;br /&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;select&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; c &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;/td&gt;&lt;br /&gt;&lt;td style="BORDER-BOTTOM: black 1pt solid; PADDING-BOTTOM: 0cm; PADDING-LEFT: 5.4pt; WIDTH: 290.6pt; PADDING-RIGHT: 5.4pt; BORDER-TOP-STYLE: none; HEIGHT: 173.25pt; BORDER-LEFT-STYLE: none; BORDER-RIGHT: black 1pt solid; PADDING-TOP: 0cm; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; mso-border-bottom-themecolor: text1color:text1;" valign="top" width="452" &gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;Country equals “USA” and additional criteria on Customer’s “first” Order using “First()” extension method. c.Orders.First() return the first order of that Customer as a Customer object where you can check any property (field, column). &lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;Another thing to note here is that we use strongly typed .Net DateTime() without doing any tricky conversion or anything like that to check OrderDate which is a DateTime in backend. If you check the SQL sent to backend “each” criteria are sent as parameters which is the way to do this in VFP too. &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;&lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;To remind Where() method, it has 2 overloads ( syntax shown here is not official and correct .Net syntax but kind of syntax that I converted to “VFP like” to make it easier VFP developers to understand input, output parameters. You can find the actual official .Net syntax on MSDN help ). &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Courier New';color:#0070c0;"&gt;Where( &lt;/span&gt;&lt;span style="font-family:'Courier New';color:red;"&gt;lFilterExpression&lt;/span&gt;&lt;span style="font-family:'Courier New';color:#0070c0;"&gt; ) &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Courier New';color:#0070c0;"&gt;Where( &lt;/span&gt;&lt;span style="font-family:'Courier New';color:red;"&gt;nRowIndex, lFilterExpression&lt;/span&gt;&lt;span style="font-family:'Courier New';color:#0070c0;"&gt; ) &lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;We have already seen how &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;i style="mso-bidi-font-style: normal"&gt;Where( lFilterExpression )&lt;/i&gt;&lt;/b&gt; version is used in the samples above. Note that, &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;c =&amp;gt; c.Country == &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"USA"&lt;/span&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt; is an expression as a whole that could be assigned to a variable (I am telling it here because I know VFP developer’s start to think how they could create dynamic expressions, do macro substitution etc. It is not exactly that but you should feel that you don’t need &amp;amp; operator. In VFP a similar one might be : lcWhere = “c.Country = ‘USA’” followed by select ... where &amp;amp;lcWhere). &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;Second overload is not supported in Linq to SQL. I will instead give a sample using an array: &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;string&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;[] months = {&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"January"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;,&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"February"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;,&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"March"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;,&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"April"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;,&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"May"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;,&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"June"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;,&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"July"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;,&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"August"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;,&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"September"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;,&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"October"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;,&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"November"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;,&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"December"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;};&lt;br /&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;var&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; myMonths = &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;from&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; m in months&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;.Where( (month, index) =&amp;gt; index % &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#c81efa;"   &gt;3&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; == &lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#c81efa;"   &gt;0&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; &amp;amp;&amp;amp; month.Contains(‘r’) )&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;select&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; m;&lt;br /&gt;myMonths.Dump(&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"Quarter starts"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;); &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;Here we use a criteria telling “Month’s array index should be a multiple of 3 and Month name should contain the letter ‘r’. We could chain multiple Where() and write a query like this: &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;var&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; inSeattle = Customers&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;.Where(c =&amp;gt; c.Country==&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"USA"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;)&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;.Where(c =&amp;gt; c.Region==&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"WA"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;)&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;.Where(c =&amp;gt; c.City==&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"Seattle"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;);&lt;br /&gt;&lt;br /&gt;inSeattle.Dump();&lt;/span&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt; &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;Which exactly generates the same SQL as this one: &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:blue;"   &gt;var&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; inSeattle = Customers&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;.Where(c =&amp;gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;c.Country==&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"USA"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; &amp;amp;&amp;amp;&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;c.Region==&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"WA"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt; &amp;amp;&amp;amp;&lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;c.City==&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:#dc1414;"   &gt;"Seattle"&lt;/span&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-FAMILY: Courier Newfont-family:Courier New;font-size:10;color:black;"   &gt;);&lt;br /&gt;&lt;br /&gt;inSeattle.Dump(); &lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-family:'Verdana','sans-serif';"&gt;&lt;/span&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-963242990221810924?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/963242990221810924/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=963242990221810924&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/963242990221810924'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/963242990221810924'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/03/query-keywords-where.html' title='Query keywords - where'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_rkddjaBL1xc/SbQkeBbWlpI/AAAAAAAAACg/uVXG7LcLua0/s72-c/linqPad_SetDatabase.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-4302658617987763518</id><published>2009-03-08T05:27:00.001+02:00</published><updated>2009-03-08T05:27:19.053+02:00</updated><title type='text'>Query keywords - from</title><content type='html'>&lt;meta name="ProgId" content="Word.Document" /&gt;&lt;meta name="Generator" content="Microsoft Word 12" /&gt;&lt;meta name="Originator" content="Microsoft Word 12" /&gt;&lt;l href="blog0308_files/filelist.xml" rel="File-List" INK&gt;&lt;l href="blog0308_files/themedata.thmx" rel="themeData" INK&gt;&lt;l href="blog0308_files/colorschememapping.xml" rel="colorSchemeMapping" INK&gt;&lt;style&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;!--&lt;br /&gt; /* Font Definitions */&lt;br /&gt; @font-face&lt;br /&gt;	{font-family:"Cambria Math";&lt;br /&gt;	panose-1:2 4 5 3 5 4 6 3 2 4;&lt;br /&gt;	mso-font-charset:162;&lt;br /&gt;	mso-generic-font-family:roman;&lt;br /&gt;	mso-font-pitch:variable;&lt;br /&gt;	mso-font-signature:-1610611985 1107304683 0 0 159 0;}&lt;br /&gt;@font-face&lt;br /&gt;	{font-family:calibri;&lt;br /&gt;	panose-1:2 15 5 2 2 2 4 3 2 4;&lt;br /&gt;	mso-font-charset:162;&lt;br /&gt;	mso-generic-font-family:swiss;&lt;br /&gt;	mso-font-pitch:variable;&lt;br /&gt;	mso-font-signature:-1610611985 1073750139 0 0 159 0;}&lt;br /&gt;@font-face&lt;br /&gt;	{font-family:verdana;&lt;br /&gt;	panose-1:2 11 6 4 3 5 4 4 2 4;&lt;br /&gt;	mso-font-charset:162;&lt;br /&gt;	mso-generic-font-family:swiss;&lt;br /&gt;	mso-font-pitch:variable;&lt;br /&gt;	mso-font-signature:536871559 0 0 0 415 0;}&lt;br /&gt;@font-face&lt;br /&gt;	{font-family:consolas;&lt;br /&gt;	panose-1:2 11 6 9 2 2 4 3 2 4;&lt;br /&gt;	mso-font-charset:162;&lt;br /&gt;	mso-generic-font-family:modern;&lt;br /&gt;	mso-font-pitch:fixed;&lt;br /&gt;	mso-font-signature:-1610611985 1073750091 0 0 159 0;}&lt;br /&gt; /* Style Definitions */&lt;br /&gt; p.msonormal, li.msonormal, div.msonormal&lt;br /&gt;	{mso-style-unhide:no;&lt;br /&gt;	mso-style-qformat:yes;&lt;br /&gt;	mso-style-parent:"";&lt;br /&gt;	margin-top:0cm;&lt;br /&gt;	margin-right:0cm;&lt;br /&gt;	margin-bottom:10.0pt;&lt;br /&gt;	margin-left:0cm;&lt;br /&gt;	line-height:115%;&lt;br /&gt;	mso-pagination:widow-orphan;&lt;br /&gt;	font-size:11.0pt;&lt;br /&gt;	font-family:"Calibri","sans-serif";&lt;br /&gt;	mso-ascii-font-family:calibri;&lt;br /&gt;	mso-ascii-theme-font:minor-latin;&lt;br /&gt;	mso-fareast-font-family:calibri;&lt;br /&gt;	mso-fareast-theme-font:minor-latin;&lt;br /&gt;	mso-hansi-font-family:calibri;&lt;br /&gt;	mso-hansi-theme-font:minor-latin;&lt;br /&gt;	mso-bidi-font-family:"Times New Roman";&lt;br /&gt;	mso-bidi-theme-font:minor-bidi;&lt;br /&gt;	mso-fareast-language:en-us;}&lt;br /&gt;.msochpdefault&lt;br /&gt;	{mso-style-type:export-only;&lt;br /&gt;	mso-default-props:yes;&lt;br /&gt;	mso-ascii-font-family:calibri;&lt;br /&gt;	mso-ascii-theme-font:minor-latin;&lt;br /&gt;	mso-fareast-font-family:calibri;&lt;br /&gt;	mso-fareast-theme-font:minor-latin;&lt;br /&gt;	mso-hansi-font-family:calibri;&lt;br /&gt;	mso-hansi-theme-font:minor-latin;&lt;br /&gt;	mso-bidi-font-family:"Times New Roman";&lt;br /&gt;	mso-bidi-theme-font:minor-bidi;&lt;br /&gt;	mso-fareast-language:en-us;}&lt;br /&gt;.msopapdefault&lt;br /&gt;	{mso-style-type:export-only;&lt;br /&gt;	margin-bottom:10.0pt;&lt;br /&gt;	line-height:115%;}&lt;br /&gt;@page section1&lt;br /&gt;	{size:595.3pt 841.9pt;&lt;br /&gt;	margin:70.85pt 70.85pt 70.85pt 70.85pt;&lt;br /&gt;	mso-header-margin:35.4pt;&lt;br /&gt;	mso-footer-margin:35.4pt;&lt;br /&gt;	mso-paper-source:0;}&lt;br /&gt;div.section1&lt;br /&gt;	{page:section1;}&lt;br /&gt;--&gt;&lt;/style&gt;&lt;d class="Section1" IV&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-family: &amp;#39;Verdana&amp;#39;,&amp;#39;sans-serif&amp;#39;"&gt;Let&lt;/span&gt;&lt;span style="font-family: &amp;#39;Verdana&amp;#39;,&amp;#39;sans-serif&amp;#39;; mso-ansi-language: en-us" lang="EN-US"&gt;’s &lt;/span&gt;&lt;span style="font-family: &amp;#39;Verdana&amp;#39;,&amp;#39;sans-serif&amp;#39;"&gt;start to check each query keyword with samples. &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-family: &amp;#39;Verdana&amp;#39;,&amp;#39;sans-serif&amp;#39;"&gt;from: Is like the “from” in SQL, refers to the source. &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #0070c0"&gt;from &lt;/span&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: red"&gt;c &lt;/span&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #0070c0"&gt;in &lt;/span&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: red"&gt;Customers&lt;/span&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #0070c0"&gt;      &lt;br /&gt;from &lt;/span&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: red"&gt;n &lt;/span&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #0070c0"&gt;in &lt;/span&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: red"&gt;numbersArray&lt;/span&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #0070c0"&gt;      &lt;br /&gt;from &lt;/span&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: red"&gt;f &lt;/span&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #0070c0"&gt;in &lt;/span&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: red"&gt;Directory.GetFiles(“c:\\temp”)&lt;/span&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #0070c0"&gt;      &lt;br /&gt;from &lt;/span&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: red"&gt;p &lt;/span&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #0070c0"&gt;in &lt;/span&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: red"&gt;Diagnostics.Process.GetProcesses()&lt;/span&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #0070c0"&gt;      &lt;br /&gt;from &lt;/span&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: red"&gt;di&lt;/span&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #0070c0"&gt; in&lt;/span&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: red"&gt; DriveInfo.GetDrives()&lt;/span&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #0070c0"&gt; &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-family: &amp;#39;Verdana&amp;#39;,&amp;#39;sans-serif&amp;#39;"&gt;In these incomplete samples c, n, f, p, di&lt;span style="mso-spacerun: yes"&gt;&amp;#160; &lt;/span&gt;are variables referring to an object of type it is used with. For example if Customers is a collection of Customer objects then “c” in “from c in Customers” refer to a Customer type. In SQL, you may think of it as the local alias of table like: &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #0070c0"&gt;Select c.CustomerID, c.CompanyName, c.ContactName from Customers c &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-family: &amp;#39;Verdana&amp;#39;,&amp;#39;sans-serif&amp;#39;"&gt;In method syntax as far as I know there isn’t something to use instead (you have it as a parameter – this Ienumerable&amp;lt;Tsource&amp;gt; source). The reason that you first write “from” is intellisense support. That makes sense if you think how we write an SQL select. Think that you have 50 columns in a table and you want to select 10 of them, visualize the ideal way that you could write it. We all have some ways to make the life easier fro ourselvees in writing those type of queries but none of them would be as good as getting a direct support from the IDE itself. Saying it like: &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #0070c0"&gt;from Customers c Select c.CustomerID, c.CompanyName, c.ContactName &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-family: &amp;#39;Verdana&amp;#39;,&amp;#39;sans-serif&amp;#39;"&gt;we would be getting help from intellisense when writing &lt;b style="mso-bidi-font-weight: normal"&gt;c.CustomerID, c.CompanyName, c.ContactName&lt;/b&gt;. I think it was a syntax choice (probably to match natural English) to write it as: &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #0070c0"&gt;from c in Customers &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-family: &amp;#39;Verdana&amp;#39;,&amp;#39;sans-serif&amp;#39;"&gt;instead of &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #0070c0"&gt;from Customers c &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-family: &amp;#39;Verdana&amp;#39;,&amp;#39;sans-serif&amp;#39;"&gt;Unlike SQL where there is a single “from” clause in Linq you can have multiple. Multiple “from” create a type of join and there is also “join” keyword. Here are samples for multiple “from”: &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #0070c0"&gt;from c in Customers from o in c.Orders &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-family: &amp;#39;Verdana&amp;#39;,&amp;#39;sans-serif&amp;#39;"&gt;is an implicit join (equijoin, inner join) – in case of Linq to SQL and Linq to Entities it infers the join as c.CustomerId = o.CustomerId). If you make a typo (might be intentional but generally a newbie typo) and write it as: &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #0070c0"&gt;from c in Customers from o in Orders &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-family: &amp;#39;Verdana&amp;#39;,&amp;#39;sans-serif&amp;#39;"&gt;then it is still an implicit join (non equijoin, cross join) but selects all records from both tables and match each producing cartesian result – same as if we do a typo in SQL version like: &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; color: #0070c0"&gt;select * from Customers, Orders&lt;span style="mso-spacerun: yes"&gt;&amp;#160; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-family: &amp;#39;Verdana&amp;#39;,&amp;#39;sans-serif&amp;#39;"&gt;leaving out the join (either with a join clause or where specifying the relation) ends in a cross join. It might be intentional, say to create a calendar like: &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-family: &amp;#39;Verdana&amp;#39;,&amp;#39;sans-serif&amp;#39;"&gt;2007,January      &lt;br /&gt;2007,February       &lt;br /&gt;...       &lt;br /&gt;2009,January       &lt;br /&gt;2009,February       &lt;br /&gt;... &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="line-height: 115%; font-family: consolas; color: black; font-size: 10pt; mso-bidi-font-family: consolas"&gt;int[] years = {&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: #c81efa; font-size: 10pt; mso-bidi-font-family: consolas"&gt;2007&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: black; font-size: 10pt; mso-bidi-font-family: consolas"&gt;,&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: #c81efa; font-size: 10pt; mso-bidi-font-family: consolas"&gt;2008&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: black; font-size: 10pt; mso-bidi-font-family: consolas"&gt;,&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: #c81efa; font-size: 10pt; mso-bidi-font-family: consolas"&gt;2009&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: black; font-size: 10pt; mso-bidi-font-family: consolas"&gt;};      &lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: blue; font-size: 10pt; mso-bidi-font-family: consolas"&gt;string&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: black; font-size: 10pt; mso-bidi-font-family: consolas"&gt;[] months = {&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: #dc1414; font-size: 10pt; mso-bidi-font-family: consolas"&gt;&amp;quot;January&amp;quot;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: black; font-size: 10pt; mso-bidi-font-family: consolas"&gt;,&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: #dc1414; font-size: 10pt; mso-bidi-font-family: consolas"&gt;&amp;quot;February&amp;quot;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: black; font-size: 10pt; mso-bidi-font-family: consolas"&gt;,&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: #dc1414; font-size: 10pt; mso-bidi-font-family: consolas"&gt;&amp;quot;March&amp;quot;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: black; font-size: 10pt; mso-bidi-font-family: consolas"&gt;,      &lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: #dc1414; font-size: 10pt; mso-bidi-font-family: consolas"&gt;&amp;quot;April&amp;quot;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: black; font-size: 10pt; mso-bidi-font-family: consolas"&gt;,&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: #dc1414; font-size: 10pt; mso-bidi-font-family: consolas"&gt;&amp;quot;May&amp;quot;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: black; font-size: 10pt; mso-bidi-font-family: consolas"&gt;,&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: #dc1414; font-size: 10pt; mso-bidi-font-family: consolas"&gt;&amp;quot;June&amp;quot;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: black; font-size: 10pt; mso-bidi-font-family: consolas"&gt;,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: #dc1414; font-size: 10pt; mso-bidi-font-family: consolas"&gt;&amp;quot;July&amp;quot;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: black; font-size: 10pt; mso-bidi-font-family: consolas"&gt;,&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: #dc1414; font-size: 10pt; mso-bidi-font-family: consolas"&gt;&amp;quot;August&amp;quot;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: black; font-size: 10pt; mso-bidi-font-family: consolas"&gt;,&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: #dc1414; font-size: 10pt; mso-bidi-font-family: consolas"&gt;&amp;quot;September&amp;quot;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: black; font-size: 10pt; mso-bidi-font-family: consolas"&gt;,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: #dc1414; font-size: 10pt; mso-bidi-font-family: consolas"&gt;&amp;quot;October&amp;quot;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: black; font-size: 10pt; mso-bidi-font-family: consolas"&gt;,&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: #dc1414; font-size: 10pt; mso-bidi-font-family: consolas"&gt;&amp;quot;November&amp;quot;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: black; font-size: 10pt; mso-bidi-font-family: consolas"&gt;,&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: #dc1414; font-size: 10pt; mso-bidi-font-family: consolas"&gt;&amp;quot;December&amp;quot;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: black; font-size: 10pt; mso-bidi-font-family: consolas"&gt;};      &lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: blue; font-size: 10pt; mso-bidi-font-family: consolas"&gt;var&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: black; font-size: 10pt; mso-bidi-font-family: consolas"&gt; myCalendarHeaders =      &lt;br /&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: blue; font-size: 10pt; mso-bidi-font-family: consolas"&gt;from&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: black; font-size: 10pt; mso-bidi-font-family: consolas"&gt; y in years      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: blue; font-size: 10pt; mso-bidi-font-family: consolas"&gt;from&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: black; font-size: 10pt; mso-bidi-font-family: consolas"&gt; m in months      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: blue; font-size: 10pt; mso-bidi-font-family: consolas"&gt;select&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: black; font-size: 10pt; mso-bidi-font-family: consolas"&gt; String.Format(&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: #dc1414; font-size: 10pt; mso-bidi-font-family: consolas"&gt;&amp;quot;{0},{1}&amp;quot;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: consolas; color: black; font-size: 10pt; mso-bidi-font-family: consolas"&gt;, y,m);      &lt;br /&gt;myCalendarHeaders.Dump(); &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-family: &amp;#39;Verdana&amp;#39;,&amp;#39;sans-serif&amp;#39;"&gt;&lt;/span&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-4302658617987763518?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/4302658617987763518/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=4302658617987763518&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/4302658617987763518'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/4302658617987763518'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/03/query-keywords-from.html' title='Query keywords - from'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-6911001051645614223</id><published>2009-03-06T04:38:00.002+02:00</published><updated>2009-03-06T04:48:14.526+02:00</updated><title type='text'>Query or method syntax</title><content type='html'>&lt;p&gt;There are a series of &lt;a href="http://msdn.microsoft.com/en-us/library/bb310804.aspx" target="_blank"&gt;"Query Keywords"&lt;/a&gt; which most of them should be familiar to any VFP developer. They are very similar to the SQL clauses we are familiar with and results they provide are more or less similar. Beside query keywords, there are a series of extension methods defined in Linq namespace with same or similar names to those keywords and more. As said before you can use query syntax most of the time and need to use method syntax in some cases where query syntax is not supported. That is what language supports and forces you to do. IMHO however, method syntax is easier and nicer. Once you get a hang of it, query syntax and the SQL we know sounds to be a hard language:) I will try to go with both syntaxes but might honor method syntax more. &lt;/p&gt;&lt;p&gt;To understand the method syntax and to use it effectively we need to learn how to read method signature. Here are the signatures of System.Linq.Enumerable.Where method as an example:&lt;/p&gt;&lt;pre&gt;public static IEnumerable&amp;lt;TSource&amp;gt; Where&amp;lt;TSource&amp;gt;(&lt;br /&gt; this IEnumerable&amp;lt;TSource&amp;gt; source,&lt;br /&gt; Func&amp;lt;TSource, bool&amp;gt; predicate&lt;br /&gt;)&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;public static IEnumerable&amp;lt;TSource&amp;gt; Where&amp;lt;TSource&amp;gt;(&lt;br /&gt; this IEnumerable&amp;lt;TSource&amp;gt; source,&lt;br /&gt; Func&amp;lt;TSource, int, bool&amp;gt; predicate&lt;br /&gt;)&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Syntax is not easy to understand but don't be afraid. If you can rewrite it in your mind unofficially you can understand it quicker then you expect:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;There are 2 signatures or two 'overloads' of Where method, meaning it could be called with 2 parameter sets. &lt;/li&gt;&lt;li&gt;&amp;lt;TSource&amp;gt; : Is all about generic typing. In case of &amp;lt;Customer&amp;gt; all those TSource become Customer type. &lt;/li&gt;&lt;li&gt;public static IEnumerable&amp;lt;TSource&amp;gt; : read this part as "it would return a series of records" – TSource is the type of Object. Think it like a table. If table is Customers then TSource is a Customers row where fields are its properties. &lt;/li&gt;&lt;li&gt;Input parameters are (for first overload):&lt;/li&gt;&lt;/ul&gt;&lt;ol&gt;&lt;li&gt;this IEnumerable&amp;lt;TSource&amp;gt; source : Parameter name is "source" and it is a collection of TSource objects (think Customer as sample). "this" tells you that it is an extension method and can be used on "this" instance (of Customer type) just like as VFP's "this". &lt;/li&gt;&lt;li&gt;Func&amp;lt;TSource, bool&amp;gt; predicate : Parameter name is predicate and it is an inline method (a lambda expression) that takes an object of type TSource (say Customer) as input and returns a bool result (true or false).&lt;br /&gt;&lt;br /&gt;In second overload, second parameter is - &lt;/li&gt;&lt;li&gt;Func&amp;lt;TSource, int, bool&amp;gt; predicate : Parameter name is again "predicate", which is an inline method that takes an object type of TSource, plus an integer as input parameters and returns a boolean value.&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Lets recap it in a pseudo code and a little bit VFP like typing:&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New;font-size:85%;color:#0000ff;"&gt;procedure Where( source as CollectionOfTSource ) as CollectionOfTSource&lt;br /&gt;&lt;br /&gt;Loop and call predicate( TSource ) per item in source Collection&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Add those that returns True to return Set&lt;/span&gt; &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Courier New;font-size:85%;color:#0000ff;"&gt;Procedure predicate( item as TSource ) as Logical&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;in second overload procedure named "predicate" would look like:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Courier New;font-size:85%;color:#0000ff;"&gt;Procedure predicate( item as TSource, parameter2 as integer ) as Logical&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Let's give an example to understand better:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Courier New;font-size:85%;color:#0000ff;"&gt;int[] numbers = {1,10,29,5,53,33,32,277,232,32,43}; &lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Courier New;font-size:85%;color:#0000ff;"&gt;IEnumerable&amp;lt;int&amp;gt; oddNumbers = from n in numbers where n % 2 != 0 select n;&lt;br /&gt;&lt;br /&gt;oddNumbers.Dump("Odd numbers using query syntax");&lt;/span&gt; &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This is vanilla ice query syntax that looks like an SQL and correspond to first overload of Where() method. Here we use "where" clause with a simple filter. Same query can be done with method syntax like this:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Courier New;font-size:85%;color:#0000ff;"&gt;IEnumerable&amp;lt;int&amp;gt; odds = System.Linq.Enumerable.Where( numbers,n =&amp;gt; n % 2 != 0 );&lt;br /&gt;&lt;br /&gt;odds.Dump("Odd numbers using method syntax - 1");&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;We are sending "numbers" as ( IEnumerable&amp;lt;TSource&amp;gt; source ) parameter and " n =&amp;gt; n % 2 != 0 " as ( Func&amp;lt;TSource, bool&amp;gt; predicate ) parameter to Where() method defined in System.Linq.Enumerable class. TSource is defined as int in this case. Since "&lt;strong&gt;Where&lt;/strong&gt;" method is defined as an extension method for IEnumerable&amp;lt;TSource&amp;gt; (in other words a collection of same type of objects – their type is referred to as TSource, in our sample TSource is &lt;strong&gt;int&lt;/strong&gt; to remind) we could use the Where method directly on the collection instance (our collection here is "numbers" which is a collection of int). Here is our simplified version:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Courier New;font-size:85%;color:#0000ff;"&gt;IEnumerable&amp;lt;int&amp;gt; odds2 = numbers.Where( n =&amp;gt; n % 2 != 0 );&lt;br /&gt;&lt;br /&gt;odds2.Dump("Odd numbers using method syntax - 2");&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Courier New;font-size:85%;color:#0000ff;"&gt;// Could write those 2 lines as one – didn't do like that just to prevent confusion:&lt;br /&gt;&lt;br /&gt;// numbers.Where( n =&amp;gt; n % 2 != 0 ).Dump("Odd number using method syntax - 2");&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Method syntax is easier IMHO and moreover it lets us to use the second overload of Where(). In second overload "int" parameter is an index to item's location (in forums you should have seen questions like: "how can I get last 3 orders per customer", this parameter lets you go further and in your inline method you can have criteria for the position – not supported on all Linq flavors, i.e.: Linq to SQL doesn't support it). Let's use it in our next sample to select odd numbers from our set "where" the number's position in array is greater than 3rd element and less than 6th element (remember in C# sequences start at 0):&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Courier New;font-size:85%;color:#0000ff;"&gt;numbers.Where( (n, i) =&amp;gt; n % 2 != 0 &amp;amp;&amp;amp; i &amp;gt; 2 &amp;amp;&amp;amp; i &amp;lt; 5 ).Dump("Odd numbers from a subset");&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Note that we didn't do any sorting (yet) last query would return 5,53. Play with simple queries using Where() method to get used to it, you wouldn't regret you learned method syntax (especially when you see chained multiple methods which we don't see in SQL). You can mix query and method syntax. By the way, whatever syntax you use intellisense kicks in and helps you on the way.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;PS: n =&amp;gt; n % 2 != 0 is a lambda expression and is typed (actually its type name is LambdaExpression). Typed &amp;gt; has a type (class) &amp;gt; is reusable, can be saved, loaded, passed around as a parameter (not something that we see with SQL and for that reason I think hard to understand initially).&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-6911001051645614223?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/6911001051645614223/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=6911001051645614223&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/6911001051645614223'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/6911001051645614223'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/03/query-or-method-syntax.html' title='Query or method syntax'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-6417771370468715247</id><published>2009-02-27T04:40:00.001+02:00</published><updated>2009-02-27T04:40:13.336+02:00</updated><title type='text'>Simplest table and query</title><content type='html'>&lt;p&gt;We made a head start to Linq skipping its basics and gave a sample from Entity Framework. Both Linq and Entity Framework are huge and much more capable than that sample could show. Linq can be done against any enumerable source means that we can treat a much wider set of source as ‘data’ and query over them using the same set of methods.&amp;#160; Linq have two syntaxes, one called Query syntax and the other method syntax. You can use the one you like but some features are only supported through method syntax (and as you get used you it method syntax sounds to be easier). &lt;/p&gt;  &lt;p&gt;We will use LinqPad for a series of reasons:&lt;/p&gt;  &lt;ul&gt;&lt;l  I&gt;A cool interface that looks similar to and can use in place of SQL server management studio. &lt;l  I&gt;We can interactively write and run C# (or VB) expressions, statements, program or even T-SQL. We can have multiple of them active at the same time shown under different tabs. &lt;l  I&gt;It has a nice way of showing the results in grids (most of the time grids). Those grids also show the power of .Net from another aspect. &lt;l  I&gt;It automatically writes in additional syntaxes and IL code where appropriate (i.e.: even if you wouldn’t use .Net at all and good in object query more than you are in T-SQL, you could write your query in Linqpad, test and get the corresponding T-SQL code from results’ window SQL tab. Below is a sample)&lt;/ul&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;from c in Customers      &lt;br /&gt;&amp;#160; where c.Country == &amp;quot;USA&amp;quot; &amp;amp;&amp;amp; c.Orders.Any(o =&amp;gt; o.OrderDate == new DateTime(1997,1,1))       &lt;br /&gt;&amp;#160; select c&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;is simplier than writing corresponding T-SQL and more natural and shows what is really sent to SQL server. Here is what it produced&amp;#160; on my box against SQLExpress 2005, Northwind sample database:&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;font color="#000080"&gt;SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax]          &lt;br /&gt;FROM [Customers] AS [t0]           &lt;br /&gt;WHERE ([t0].[Country] = @p0) AND (EXISTS(           &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; SELECT NULL AS [EMPTY]           &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; FROM [Orders] AS [t1]           &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; WHERE ([t1].[OrderDate] = @p1) AND ([t1].[CustomerID] = [t0].[CustomerID])           &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ))           &lt;br /&gt;&lt;/font&gt;&lt;font color="#008000"&gt;-- @p0: Input NVarChar (Size = 3; Prec = 0; Scale = 0) [USA]          &lt;br /&gt;-- @p1: Input DateTime (Size = 0; Prec = 0; Scale = 0) [01.01.1997 00:00:00]           &lt;br /&gt;-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.30729.1&lt;/font&gt;&lt;/font&gt;&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;You could write it in multiple ways and interestingly enough the code generated is almost the exact query that I would suggest if I saw someone asking something like: &lt;/p&gt;  &lt;p&gt;“Using Northwind data, how could I get the customers who are from USA and had an order on 1/1/1997?” &lt;/p&gt;  &lt;ul&gt;&lt;l  I&gt;Saving, loading of your own codes is a snap. &lt;l  I&gt;With its samples tab it is not simply a tool but a tutorial in itself. &lt;l  I&gt;Assuming you picked the autocomplete activation for a few bucks, it simplifies the writing of code as if you are in VS.&lt;/ul&gt;  &lt;p&gt;In summary, LinqPad is a cool utility deserving more than this promotional words. Let’s put up the LinqPad and start to investigate. LinqPad’s samples are nice, do not forget to check them out.&lt;/p&gt;  &lt;p&gt;The simplest enumerable thing would be an array. Starting with an integer array lets check Linq syntax, extension methods and others:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#0000ff"&gt;int[] digits = {0,1,2,3,4,5,6,7,8,9};      &lt;br /&gt;var query = &lt;font color="#ff0000"&gt;from&lt;/font&gt; d in &lt;font color="#0000a0"&gt;digits&lt;/font&gt; &lt;font color="#ff0000"&gt;select&lt;/font&gt; d; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#0000ff"&gt;foreach(var digit in query)      &lt;br /&gt;{       &lt;br /&gt;&amp;#160; Console.WriteLine(digit);       &lt;br /&gt;}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;It is a lot of code, yes., but of course it is because I started with a long and verbose code. Now I will make analogies to SQL but Linq is not SQL, it might be strange to compare them (you better try to think in terms of objects and gets accustomed to it ASAP). You can think of “digits” as a single column table. The simplest select query would be:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;select * from digits&lt;/strong&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;In Linq query syntax we start with “from” and:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;from d in digits&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;is a way of saying - OK I want ‘rows’ from ‘digits’ table and I will refer to each ‘row instance’ as ‘d’. In that sense, ‘select d’ means ‘all columns of’ or ‘select *’. Assigning it to ‘query’ is like ‘into cursor query’ (with the exception ‘select … into cursor query’ wouldn’t really execute until foreach(…) that demands its result – foreach() is not the only way to demand results).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;foreach(var digit in query) { // do whatever }&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;is a simple loop which you can think of as ‘for each … endfor’ or ‘scan…endscan’. In VFP that code would be something like:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;select * from digits into cursor query &amp;amp;&amp;amp; assuming we have a single column named d      &lt;br /&gt;scan       &lt;br /&gt;&amp;#160;&amp;#160; ? d &amp;amp;&amp;amp; or query.d       &lt;br /&gt;endscan&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Hmm, you say, but in VFP I have the luxury of using xbase and could simply do it like this:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;use digits      &lt;br /&gt;scan       &lt;br /&gt;&amp;#160;&amp;#160; ? d       &lt;br /&gt;endscan&amp;#160; &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;In C# you didn’t really need that ‘from … ‘ and could simply use ‘digits’ itself in the same manner:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#0000ff"&gt;int[] digits = {0,1,2,3,4,5,6,7,8,9};      &lt;br /&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff"&gt;foreach(var digit in digits)      &lt;br /&gt;{       &lt;br /&gt;&amp;#160; Console.WriteLine(digit);       &lt;br /&gt;}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;This is plain old C#, no Linq in sight. However, if it were a table like thing we would expect to be able to do filtered selections, aggregations like average, sum… etc. If you didn't pay for LinqPad's autocomplete activation do this in VS:&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#0000ff"&gt;int[] digits = {0,1,2,3,4,5,6,7,8,9};      &lt;br /&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff"&gt;var myCursor = digits.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;As soon as you put the dot intellisense would kick in (in case of VS it would kick in earlier) and list PEM of digits. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_rkddjaBL1xc/SadSeoNe5SI/AAAAAAAAACA/GlCoKdlBtzE/s1600-h/Linq1_Intellisense%5B7%5D.jpg"&gt;&lt;img title="Linq1_Intellisense" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="420" alt="Linq1_Intellisense" src="http://lh3.ggpht.com/_rkddjaBL1xc/SadSfoFpN1I/AAAAAAAAACE/22ZacmTm8KU/Linq1_Intellisense_thumb%5B3%5D.jpg?imgmax=800" width="508" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Methods are shown with a pink icon and those methods having blue arrow next to pink icon are Extension Methods defined in Linq namespace. If you disable Linq extensions by commenting using System.Linq you see those methods are gone from the list:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_rkddjaBL1xc/SadSgvrkrJI/AAAAAAAAACI/YCexFiN6HQ0/s1600-h/Linq2_NoIntellisense%5B3%5D.jpg"&gt;&lt;img title="Linq2_NoIntellisense" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="424" alt="Linq2_NoIntellisense" src="http://lh6.ggpht.com/_rkddjaBL1xc/SadShesttXI/AAAAAAAAACM/K_Uo2PP7mrE/Linq2_NoIntellisense_thumb%5B1%5D.jpg?imgmax=800" width="522" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;In LinqPad some namespaces you need frequently are implicitly imported and you don't need those using lines – and you could add other namespaces you may need using F4. In LinqPad there is a cool method named &amp;quot;Dump()&amp;quot; that &amp;quot;eats&amp;quot; anything you throw at it! Use it to easily check your results and play with 'digits table' in LinqPad, try things like:&lt;/p&gt;  &lt;p&gt;digits.First().Dump();    &lt;br /&gt;digits.Sum().Dump(&amp;quot;Sum of Digits&amp;quot;);     &lt;br /&gt;digits.Average().Dump();&lt;/p&gt;  &lt;p&gt;or have more fun with queries like:&lt;/p&gt;  &lt;p&gt;int[] digits = {0,1,2,3,4,5,6,7,8,9};    &lt;br /&gt;var query1 = from d in digits where d &amp;gt; 5 select d;     &lt;br /&gt;query1.Dump();     &lt;br /&gt;var query2 = from d in digits where d % 2 == 0 orderby d descending select d;     &lt;br /&gt;query2.Dump();     &lt;br /&gt;var query3 =     &lt;br /&gt;&amp;#160; from d in digits     &lt;br /&gt;&amp;#160; select new {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; d,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; even = (d % 2 == 0),     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; sqr = d * d,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cube = d * d * d,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; bitset = 1 &amp;lt;&amp;lt; d     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; };     &lt;br /&gt;query3.Dump();     &lt;br /&gt;var query4 =     &lt;br /&gt;&amp;#160; from dColumns in query3     &lt;br /&gt;&amp;#160; group dColumns by dColumns.even into groupedCursor     &lt;br /&gt;&amp;#160; select groupedCursor;     &lt;br /&gt;query4.Dump();&lt;/p&gt;  &lt;ul&gt;&lt;l  I&gt;query1 adds a simple 'where' clause. &lt;l  I&gt;query2 demonstrates an 'order by'. &lt;l  I&gt;query3 demonstrates projection (like SQL's as clause – select d, (d%2 = 0) as even, d*d as sqr … ) &lt;l  I&gt;query4 demonstrates querying from another query (query3 – like making a select from a previous cursor) plus 'group by'. Note that group by result is a little different than SQL group by result and is a Key, Collection list ( like a table of grouping keys that contain their related items ):&lt;/ul&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_rkddjaBL1xc/SadSiDg-kDI/AAAAAAAAACQ/jpHa1-yfLOQ/s1600-h/Linq3_Grouping%5B6%5D.jpg"&gt;&lt;img title="Linq3_Grouping" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="395" alt="Linq3_Grouping" src="http://lh3.ggpht.com/_rkddjaBL1xc/SadSjFPfzzI/AAAAAAAAACU/58uNF6OzNJM/Linq3_Grouping_thumb%5B4%5D.jpg?imgmax=800" width="291" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Are we having fun yet? Just wet our toes:)&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-6417771370468715247?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/6417771370468715247/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=6417771370468715247&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/6417771370468715247'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/6417771370468715247'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/02/simplest-table-and-query.html' title='Simplest table and query'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/_rkddjaBL1xc/SadSfoFpN1I/AAAAAAAAACE/22ZacmTm8KU/s72-c/Linq1_Intellisense_thumb%5B3%5D.jpg?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-4941817476153376989</id><published>2009-02-20T02:06:00.001+02:00</published><updated>2009-02-20T02:40:00.223+02:00</updated><title type='text'>What if the backend wasn’t VFP</title><content type='html'>&lt;p&gt;Linq comes in various flavors like Linq to objects, Linq to XML, Linq to dataset, Linq to SQL (L2S), Linq to entities (L2E). When it is relational data Linq to Dataset, L2S and L2E are the ones we need to think about. Of those 3 Linq to dataset is the hardest IMHO. L2S, though very nice, easy and fast is somewhat outdated. L2E is newest and suppported by increasing number of providers. Check current list of &lt;a href="http://msdn.microsoft.com/en-us/data/dd363565.aspx" target="_blank"&gt;&amp;quot;ADO.NET Entity Framework Providers&amp;quot;&lt;/a&gt;. As a data backend my preference is SQL server and what I write are almost always based on SQL server. To keep long story short, instead of a DataSet we would create an ADO.Net entity model:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_rkddjaBL1xc/SZ3z5g55mEI/AAAAAAAAABw/IVzWBPaoDUY/s1600-h/EF1%5B3%5D.jpg"&gt;&lt;img title="EF1" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="425" alt="EF1" src="http://lh4.ggpht.com/_rkddjaBL1xc/SZ3z673fCFI/AAAAAAAAAB0/_q9D8K5sUxU/EF1_thumb%5B1%5D.jpg?imgmax=800" width="687" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;For a quick test, select “Generate from database”, point to SQL server Northwind database and select to create a model with Customers, Orders and Order Details tables.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_rkddjaBL1xc/SZ3z71URr3I/AAAAAAAAAB4/9Ou3AnVDxZ8/s1600-h/EF2%5B3%5D.jpg"&gt;&lt;img title="EF2" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="392" alt="EF2" src="http://lh3.ggpht.com/_rkddjaBL1xc/SZ3z85MKh-I/AAAAAAAAAB8/8LRbUXj73mU/EF2_thumb%5B1%5D.jpg?imgmax=800" width="630" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;This operation is certainly easier than creating the testdata typed dataset that we did yesterday. Save it and write the new code.&amp;#160; Here is new code:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;using System;      &lt;br /&gt;using System.Collections.Generic;       &lt;br /&gt;using System.Linq;       &lt;br /&gt;using System.Text; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;namespace L2E      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; class Program       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; static void Main(string[] args)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&lt;font color="#800080"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; NorthwindEntities nw = new NorthwindEntities(); &lt;/font&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#800080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var usaCustomers =      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; from c in nw.Customers       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; where c.Country.Trim() == &amp;quot;USA&amp;quot;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; select c;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var AComplexQuery =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; from c in nw.Customers       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; where c.Orders       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Sum(o =&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; o.Order_Details       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Sum(od =&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; od.Quantity * od.UnitPrice)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ) &amp;gt; 50000m       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; select c; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; foreach (var customer in usaCustomers)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; customer.Region = &amp;quot;1&amp;quot; + customer.Region;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; foreach (var customer in AComplexQuery)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; customer.Region = &amp;quot;2&amp;quot; + customer.Region;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&amp;quot;Easy USA Customers: {0}&amp;quot;, usaCustomers.Count());       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&amp;quot;Easy AComplexQuery: {0}&amp;quot;, AComplexQuery.Count());       &lt;br /&gt;&lt;font color="#800080"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; nw.SaveChanges(true);        &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Can you say this is harder than yesterday’s code? Isn’t this code easier than the VFP code that would do the same thing (no matter what the backend is)? OK maybe you would say not easier but close, for me much easier:) Implementation of foreach() content is shorter but not much important. However if you look at purple blocks you would see that now the query is easier and now you have a better navigational path.&lt;/p&gt;  &lt;p&gt;L2S generates SQL against backend while L2E generate a sort of SQL called “Entity SQL”. From MSDN:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Entity SQL is a &lt;strong&gt;storage-independent language&lt;/strong&gt; that is similar to SQL. Entity SQL was designed to query and manipulate rich object graphs of objects based on the Entity Data Model (EDM).&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;You can find the details of Entity SQL &lt;a href="http://msdn.microsoft.com/en-us/library/bb399560.aspx" target="_blank"&gt;here.&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-4941817476153376989?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/4941817476153376989/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=4941817476153376989&amp;isPopup=true' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/4941817476153376989'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/4941817476153376989'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/02/what-if-backend-wasnt-vfp.html' title='What if the backend wasn’t VFP'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/_rkddjaBL1xc/SZ3z673fCFI/AAAAAAAAAB0/_q9D8K5sUxU/s72-c/EF1_thumb%5B1%5D.jpg?imgmax=800' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-4371860576722664943</id><published>2009-02-19T04:52:00.001+02:00</published><updated>2009-02-19T04:52:45.452+02:00</updated><title type='text'>Linq is fun – continued, part 2</title><content type='html'>&lt;p&gt;Here is the code:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;using System;      &lt;br /&gt;using System.Collections.Generic;       &lt;br /&gt;using System.Linq;       &lt;br /&gt;using System.Text; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;namespace VFPLinq      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; class Program       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; static void Main(string[] args)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Testdata td = new Testdata();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; TestdataTableAdapters.CustomerTableAdapter daCus =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new VFPLinq.TestdataTableAdapters.CustomerTableAdapter();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; TestdataTableAdapters.ordersTableAdapter daOrd =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new VFPLinq.TestdataTableAdapters.ordersTableAdapter();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; TestdataTableAdapters.orditemsTableAdapter daOi =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new VFPLinq.TestdataTableAdapters.orditemsTableAdapter(); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; daCus.Fill(td.Customer);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; daOrd.Fill(td.orders);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; daOi.Fill(td.orditems); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var usaCustomers =      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; from c in td.Customer       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; where c.country.Trim() == &amp;quot;USA&amp;quot;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; select c; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var AComplexQuery =      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (from c in td.Customer       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; where c.GetordersRows()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Sum(o =&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; o.GetorditemsRows()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Sum(od =&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; od.quantity * od.unit_price)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ) &amp;gt; 50000m       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; select c).Distinct(); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int colWidth = td.Customer.regionColumn.MaxLength; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; foreach (var customer in usaCustomers)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; string newRegion =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; String.Format(&amp;quot;{0}{1}&amp;quot;, 1, customer.region.Trim());       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; customer.region =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; newRegion.Length &amp;gt; colWidth       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ? newRegion.Substring(0, colWidth)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; : newRegion;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; foreach (var customer in AComplexQuery)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; string newRegion =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; String.Format(&amp;quot;{0}{1}&amp;quot;, 2, customer.region.Trim());       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; customer.region =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; newRegion.Length &amp;gt; colWidth       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ? newRegion.Substring(0, colWidth)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; : newRegion;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&amp;quot;Updating {0} customers for usaCustomers&amp;quot;,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; usaCustomers.Count());       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&amp;quot;Updating {0} customer for complex query&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AComplexQuery.Count()); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; daCus.Update(td);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; td.AcceptChanges();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;If video trial wasn’t a disappointment and it didn’t drop frames becuase of the lower quality I chose, you would be able to see it is easy to write all these code with the help of intellisense. Otherwise I agree, without intellisense writing all these is not fun. Before running this code run this one in VFP:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#800080" size="2"&gt;SELECT cust_id,region,country ;      &lt;br /&gt;&amp;#160; FROM (_samples+'data\customer') ;       &lt;br /&gt;&amp;#160; WHERE ISDIGIT(region)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;It shouldn’t show any records. Go back to C# code and run it with Ctrl+F5 or “Debug\Run without debugging”. We are using Ctrl+F5 instead of simply the F5 because we want DOS screen (we created a console application) to show the results and wait for an input from us before closing (without adding a wait state code like readline – being lazy is good:)&lt;/p&gt;  &lt;p&gt;You would see updated customer counts. Go to VFP and check results running the same code again. In second run this is my screen snapshot:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_rkddjaBL1xc/SZzJdi5FA1I/AAAAAAAAABo/-BRiQk89gEw/s1600-h/TestdataStep5%5B3%5D.jpg"&gt;&lt;img title="TestdataStep5" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="664" alt="TestdataStep5" src="http://lh3.ggpht.com/_rkddjaBL1xc/SZzJe503vyI/AAAAAAAAABs/xoAudaUjWIU/TestdataStep5_thumb%5B1%5D.jpg?imgmax=800" width="413" border="0" /&gt;&lt;/a&gt;&amp;#160; &lt;/p&gt;  &lt;p&gt;You see customers matching to 1st query (USA customers are flagged with a 1 prefix and priviliged ones with a 2 prefix. Some customers are both from USA and priviliged as well they are marked with “21” prefix (we first added 1).&lt;/p&gt;  &lt;p&gt;So what? What did we do all this time? Just updated a few records based on some simple and complex (well not so complex) queries? Well that may be enough to impress some developers but certainly VFP developers would be thinking “hah, I have been doing that for years and don’t need to know all those syntax and rest”. If that weren’t enough, we first filled a dataset with all the records from our database, which means all the data would travel down the wire if it were on a remote machine (not so remote when it is VFP but still it is a network).&amp;#160; You would be right in your thoughts. However, just for a second think:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;We started with a sample that is not designed for efficiency. We just wanted to show that “we can”. Simply loaded and updated some VFP data based on some Linq queries. &lt;/li&gt;    &lt;li&gt;Linq didn’t really have some role in what we did except that:      &lt;ul&gt;       &lt;li&gt;filtering the data&amp;#160; &lt;/li&gt;        &lt;li&gt;showing what would need a relatively complex SQL could be written by a non-SQL oriented developer in an easy way to express ( I mean he doesn’t need to know what a join is and how to connect them in SQL – Linq has joins but do not think as if it were what it is in SQL). &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;VFP is not a good choice to use as a backend especially when working with .Net and expecting efficiency in coding would not be right. VFP and .Net is from the same company but VFP simply do not have the provider support we need (maybe will be added later or someone who ever could make DDEX provider work right may have had a better experience – I was lazy not to try too much with it). &lt;/li&gt;    &lt;li&gt;Dataset itself is not a good choice to show power of Linq and processing data. ADO.Net predates Linq. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Maybe I am wrong.&amp;#160; Anyway let’s dissect the code.&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Testdata td = new Testdata();      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; TestdataTableAdapters.CustomerTableAdapter daCus =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new VFPLinq.TestdataTableAdapters.CustomerTableAdapter();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; TestdataTableAdapters.ordersTableAdapter daOrd =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new VFPLinq.TestdataTableAdapters.ordersTableAdapter();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; TestdataTableAdapters.orditemsTableAdapter daOi =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new VFPLinq.TestdataTableAdapters.orditemsTableAdapter(); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; daCus.Fill(td.Customer);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; daOrd.Fill(td.orders);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; daOi.Fill(td.orditems); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;is well known “typed dataset” loading. I won’t let you get confused what a typed dataset is and explain a bit here w/o any links. In practical terms think in VFP you are selecting data from a table and you only know its name. You would do a query and check what fields you got. It is a generic, at run time discovery of what you have on hand. Updating such data would some checks on data types and column names. Or to say it in another way it is as if you are doing:&lt;/p&gt;  &lt;p&gt;SQLExec(m.lnHandle,”select * from SomeTable”,”myCursor”)&lt;/p&gt;  &lt;p&gt;and not using a cursor schema. You know the pain. You can create such datatables and datasets and it would be Untyped. When you have a dbc with tables and views in it –either local or remote- you can generate a prg version of your data structures using gendbc (or using other ways any table). There is a definition of your data in code. Likewise in .Net you can define a “dataset” with its tables, relations, data types, constraints and so on that&amp;#160; matches to its source (and generated from an existing source generally). Such a dataset is called a “typed dataset”. “Testdata” is a designer generated (dataset designer) type (remember terminology class, structs, delegates are all “type”s). &lt;/p&gt;  &lt;p&gt;Testdata td = new Testdata();&lt;/p&gt;  &lt;p&gt;is something like:&lt;/p&gt;  &lt;p&gt;td = createobject(“Testdata”)&lt;/p&gt;  &lt;p&gt;and &lt;/p&gt;  &lt;p&gt;TestdataTableAdapters.CustomerTableAdapter daCus = new VFPLinq.TestdataTableAdapters.CustomerTableAdapter();&lt;/p&gt;  &lt;p&gt;is something like:&lt;/p&gt;  &lt;p&gt;daCus = newobject(“MyCustomerCursorAdapter”, “VFPLinqTestDataTableAdapters.vcx”)&lt;/p&gt;  &lt;p&gt;DataAdapters like CursorAdapters in VFP are “adapter” types that manage from/to data store (designer already wrote SelectCommand, DeleteCommand, UpdateCommand, … for you).&amp;#160; If you are working with cursoradapter classes in VFP then it is easier to understand. Anyway, .Net handles the code for you and in fact we could have done a “zero code” 3 table (Customer, Orders, Order Details) &lt;strong&gt;updatable&lt;/strong&gt; form, just by a few (click, drag &amp;amp; drop)s. &lt;/p&gt;  &lt;p&gt;Here in code our Linq queries: &lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var usaCustomers =      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; from c in td.Customer       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; where c.country.Trim() == &amp;quot;USA&amp;quot;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; select c; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var AComplexQuery =      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (from c in td.Customer       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; where c.GetordersRows()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Sum(o =&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; o.GetorditemsRows()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Sum(od =&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; od.quantity * od.unit_price)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ) &amp;gt; 50000m       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; select c).Distinct(); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;“var” is a keyword and means C# would infer the real “type from usage”. Instead of var you may write the type explicitly yourself but var is easier and you can use as a “teacher” what the type would be (Tip: in VS after writing your query move mouse on to var). In VFP the first one looks like:&lt;/p&gt;  &lt;p&gt;text to m.usaCustomers noshow &amp;amp;&amp;amp; with ansi on – case sensitive since we are executing against VFP    &lt;br /&gt;select * from customer where trim(country) = “USA”     &lt;br /&gt;endtext&amp;#160; &lt;br /&gt;SQLExec(m.handle,m.usaCustomers)&lt;/p&gt;  &lt;p&gt;Not exactly but the point is that it is a “query definition”. Defined but not executed. Something that requires a result from that query “triggers” the execution and each execution is another call (if in between data changed from outside this code next execution brings the results based on new content – sort of requery). If you didn’t like it don’t worry it is just one of the ways you can use it. Anyway back on track, you would find the syntax very similar to SQL. This is more like OQL (Object Query Language). It starts with “from” instead of “select” and interestingly “select” is placed at the end. You would appreciate it such an arrangement lets you efficiently use intellisense on queries and “select” at the end is referred to as “selector” (in syntax documentation you would see things like selector, projector…). &lt;/p&gt;  &lt;p&gt;from c in td.Customer&lt;/p&gt;  &lt;p&gt;here “c” is just an identifier name I chose to name each td.Customer type in set. In practical terms c refers to a row of customer table as if I have done:&lt;/p&gt;  &lt;p&gt;select customer    &lt;br /&gt;scan     &lt;br /&gt;scatter name c     &lt;br /&gt;…     &lt;br /&gt;endscan     &lt;br /&gt;    &lt;br /&gt;(w/o scan …endscan execution). Or you could think of it as a local alias of a SELECT-SQL:&lt;/p&gt;  &lt;p&gt;select … from customer c … &lt;/p&gt;  &lt;p&gt;(BTW I am making analogies for you to understand initally and don’t get too much confused – you would do yourself a favor if you can stop thinking in SQL and think the Linq way but initially what I am doing shouldn’t harm you).&lt;/p&gt;  &lt;p&gt;where line in 1st query looks like and works like our classical SQL and finally select c means select * (or yield c type that we started with). From a practical view point, a simple SQL selecting the USA customers. The result being an object in fact is not your fault:)&lt;/p&gt;  &lt;p&gt;Second query is sligthtly more complex but should be easy to understand for VFP community, with the exception of Get…Rows().Sum( … ).&amp;#160; I just wanted to poke a different one initially:) It would be good to talk about it later. &lt;/p&gt;  &lt;p&gt;OK next we got region column’s maxLength aka field size. Why we ever did that? We could simply do our replace ignoring overflowed data. In VFP that would work, character fields trailing characters are not important or even if the data wouldn’t fit VFP would silently let us do the replacement dropping characters at the end. However in ADO.Net it has been thought of as a developer error and causes an exception. Instead of a try..catch I simply wanted to create a new entry that is not more than max length (see in code there is a check if it is exceeding max length).&lt;/p&gt;  &lt;p&gt;foreach() is like the for each we know in VFP and we can say that it is “scan … endscan”. Yes you are not losing scan…endscan just name and syntax if you are not too resistant to think otherwise.&amp;#160;&amp;#160; In our code it is foreach() that “triggers” the execution of related query. &lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int colWidth = td.Customer.regionColumn.MaxLength; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; foreach (var customer in usaCustomers)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; string newRegion =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; String.Format(&amp;quot;{0}{1}&amp;quot;, 1, customer.region.Trim());       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; customer.region =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; newRegion.Length &amp;gt; colWidth       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ? newRegion.Substring(0, colWidth)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; : newRegion;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; foreach (var customer in AComplexQuery)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; string newRegion =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; String.Format(&amp;quot;{0}{1}&amp;quot;, 2, customer.region.Trim());       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; customer.region =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; newRegion.Length &amp;gt; colWidth       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ? newRegion.Substring(0, colWidth)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; : newRegion;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;After setting the region value for the matches within a “scan … endscan” like iterator structure finally we show how many “records” matched using “Count()” extension method on our queries (if you hover your mouse on “var” you would see it is an IEnumarable&amp;lt;T&amp;gt; and T is VFPLinq.Testdata.CustomerRow – in other words something you can enumerate and thus already have an extension method “Count()” in Linq namespace).&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&amp;quot;Updating {0} customers for usaCustomers&amp;quot;,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; usaCustomers.Count());       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&amp;quot;Updating {0} customer for complex query&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AComplexQuery.Count()); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; daCus.Update(td);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; td.AcceptChanges();&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;daCus.Update() is like TableUpdate(2,.F.,”v_customer”)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;Hope you started to like it. If you did you would love it when you see muchmore simple and effective ways of doing these (I shouldn’t start with a though one maybe). See you in next post folks.&lt;/font&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-4371860576722664943?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/4371860576722664943/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=4371860576722664943&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/4371860576722664943'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/4371860576722664943'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/02/linq-is-fun-continued-part-2.html' title='Linq is fun – continued, part 2'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/_rkddjaBL1xc/SZzJe503vyI/AAAAAAAAABs/xoAudaUjWIU/s72-c/TestdataStep5_thumb%5B1%5D.jpg?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-5549623140900814210</id><published>2009-02-19T02:15:00.001+02:00</published><updated>2009-02-19T02:15:46.994+02:00</updated><title type='text'>Linq is Fun - continued</title><content type='html'>&lt;p&gt;OK since the video has failed let’s publish photos and explain.&lt;/p&gt;  &lt;p&gt;Start Visual studio (or express),Select C# Console application for a new project and name it say “VFPLinq”. Right click, project in solution explorer (bold) and select add\add new item and select “Dataset”, name it Testdata.xsd&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_rkddjaBL1xc/SZykjhSi7ZI/AAAAAAAAABE/wTfRZVmuauw/s1600-h/TestdataStep3%5B4%5D.jpg"&gt;&lt;img title="TestdataStep3" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="425" alt="TestdataStep3" src="http://lh4.ggpht.com/_rkddjaBL1xc/SZykkzk8xEI/AAAAAAAAABM/l0b0Mcz6QDY/TestdataStep3_thumb%5B2%5D.jpg?imgmax=800" width="687" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;This would cause a few files added to project and a new dataset design surface pop up, it may take some time, be patient. Now if server explorer window is visible select it or from menu Data\Add new data source\Database\New connection. If you are using VFP9 SP2 and registered DDEX provider you may try your chances with it. I installed it last night and it gave me a lot of trouble I won’t use it (looks like besides not thinking of VS2008 they forgot 2 dlls which doesn’t exist in VS2008 only installations, I setup VS SDK to overcome and rebuild and finally got it to work – but results were not good giving other errors about update command generation. I surrendered seeking about the reason and prefer to work with .Net data provider for OleDb instead. It works well).&amp;#160; OK anyway next step is to add the connection.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_rkddjaBL1xc/SZyklsQ5LBI/AAAAAAAAABQ/VRbSdr52Buk/s1600-h/TestdataStep1%5B7%5D.jpg"&gt;&lt;img title="TestdataStep1" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="261" alt="TestdataStep1" src="http://lh5.ggpht.com/_rkddjaBL1xc/SZykmQX_KbI/AAAAAAAAABU/WkfP8o9f3N8/TestdataStep1_thumb%5B3%5D.jpg?imgmax=800" width="442" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_rkddjaBL1xc/SZyknADSWqI/AAAAAAAAABY/aO4pWzWOhmo/s1600-h/TestdataStep2%5B3%5D.jpg"&gt;&lt;img title="TestdataStep2" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="541" alt="TestdataStep2" src="http://lh6.ggpht.com/_rkddjaBL1xc/SZykoIdtNDI/AAAAAAAAABc/RlL3G6lCYV4/TestdataStep2_thumb%5B1%5D.jpg?imgmax=800" width="514" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;You would see a new data connection added to your server explorer window, expand connection and then tables node as shown below, drag&amp;amp;drop Customer, orders, ordItems tables on to design surface. Just drop and save. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_rkddjaBL1xc/SZykpCaWBjI/AAAAAAAAABg/TbcxZKaynaU/s1600-h/TestdataStep4%5B18%5D.jpg"&gt;&lt;img title="TestdataStep4" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="587" alt="TestdataStep4" src="http://lh3.ggpht.com/_rkddjaBL1xc/SZykq0PXgqI/AAAAAAAAABk/NzA9oUWJnYY/TestdataStep4_thumb%5B16%5D.jpg?imgmax=800" width="904" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;As you would notice it automatically sets up relations. What we have done is to create a definition of our connection and data – to say it otherwise it is something like adding a connection and remote view(s) to a database designer in VFP.&lt;/p&gt;  &lt;p&gt;Now we can switch to code window (the tab shown as “program.cs”). We would write code that:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Fills up a dataset with ALL the data from customer, orders and orditems tables. &lt;/li&gt;    &lt;li&gt;Defines 2 queries      &lt;ol&gt;       &lt;li&gt;A simple one that select “USA” customers &lt;/li&gt;        &lt;li&gt;A complex one that selects priviliged customers – that is customers that made more than 50,000$ worth of purchases (total). &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt;    &lt;li&gt;Based on customers matching to those queries we will update first customer set (USA customers) region column with a “1” prefix, and 2nd one with “2” as a flag to show who we matched and that we are really updating VFP data easily. I chose updating only region column in such a fashion so it wouldn’t be a very destructive operation on your sample data. I know it is a sample database anyway but probably you wouldn’t backup at all and don’t want tyo modify much. &lt;/li&gt;    &lt;li&gt;Finally code writes to console how many records would be affected per change. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;In next post you will see code and more explanations. Hang on.&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-5549623140900814210?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/5549623140900814210/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=5549623140900814210&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/5549623140900814210'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/5549623140900814210'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/02/linq-is-fun-continued.html' title='Linq is Fun - continued'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/_rkddjaBL1xc/SZykkzk8xEI/AAAAAAAAABM/l0b0Mcz6QDY/s72-c/TestdataStep3_thumb%5B2%5D.jpg?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-2820459773547928657</id><published>2009-02-19T01:32:00.001+02:00</published><updated>2009-02-19T01:32:58.957+02:00</updated><title type='text'>Linq is fun</title><content type='html'>&lt;p align="justify"&gt;LINQ is short for “Language INtegrated Query”-&amp;#160; and it is fun.&amp;#160; When you say “Query” to a VFP developer she/he immediately visualizes a sequence like “select … from … where …” or SQL in other words. Besides SQL, VFP developers work with xbase commands a lot and don’t like the idea to work w/o having scan…endscan, locate … etc series of commands. I think VFP developers’ majority saw .Net as an hostile environment especially on data processing area and IMHO they were right until 3.0 came out. Now they can feel at home (not so quite, somethimes it will be hard to grasp, sometimes you would love thinking it is much easier then SQL). I get some questions about performance, how do I … etc and I hope to answer those questions as we go.&amp;#160; &lt;/p&gt;  &lt;p align="justify"&gt;BTW to learn Linq I read books like &lt;a href="http://microsofteref.books24x7.com/toc.asp?bkid=18629"&gt;Introducing Microsoft LINQ by Paolo Pialorsi and Marco Russo Microsoft Press © 2007&lt;/a&gt; and &lt;a href="http://microsofteref.books24x7.com/toc.asp?bkid=24506"&gt;Programming Microsoft LINQ by Paolo Pialorsi and Marco Russo Microsoft Press © 2008&lt;/a&gt; as well as &lt;a href="http://www.albahari.com/nutshell/" target="_blank"&gt;C# 3.0 in a Nutshell by Joseph Albahari and Ben Albahari&lt;/a&gt; (and actually still reading). Those books are good (well good is not enough to describe, especially #.0 in a Nutshell is one of the best I have ever read) but just reading and testing in Visual Studio is an hard and slow way of learning. I happened to be interested in F# in between and honestly working with it missing pieces of the puzzle started to find their places faster for me, especially lambda expressions and statements. And of course it was the &lt;a href="http://www.linqpad.net/" target="_blank"&gt;LinqPad&lt;/a&gt; utility that made my learning roll faster. So did I learn it? No, not yet, I still have way to go but I have come a long way I think. Enough story:) Let’s kick off.&lt;/p&gt;  &lt;p align="justify"&gt;Linq is about querying but that doesn’t mean it only helps to query (in other words to select). Using Linq we do CRUD operations ( CreateRetrieveUpdateDelete or Insert/Select/Update/Delete) and even some DML operations using classes that are built to support Linq. Practically speaking it makes the VFP developers’ life easier in .Net :) In VFP “query” reminds us tables/cursors. What is cool about Linq is that you can “query” anything that can be enumerated or queried, in .Net terms those that implement IEnumerable&amp;lt;T&amp;gt; or IQueryable&amp;lt;T&amp;gt;. What does it mean? Practically it means you can query structures like tables, arrays, collections …&amp;#160; IOW almost anything that has a plural form. Seeing is believeing. Launch LinqPad. If you check “Samples” tab you would see that Joseph already took the show in a 5 minute induction (and of course other cool samples in following nodes). We will use LinqPad mostly because it is easy to test code there and save or discard as you wish. &lt;/p&gt;  &lt;p&gt;Now we will go and directly query/update/delete some data in VFP samples\data\tesdata.dbc ! The reason that I start with such an aggressive sample is that I need to showcase as early as possible to some forum bloggers/posters that .Net is not as bad as they think in data processing. I recorded a video of what I did. I will upload it to here, plus explain what we did and why etc. I will also give the code. OK here we go.&lt;/p&gt;  &lt;p&gt;(Video didn’t work good – updating to support with photos)&lt;/p&gt;  &lt;p&gt;I am not sure if video would work (my first attempt). If it doesn’t I will support most important parts with photos. Let me post this and see.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-2820459773547928657?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/2820459773547928657/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=2820459773547928657&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/2820459773547928657'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/2820459773547928657'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/02/linq-is-fun.html' title='Linq is fun'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-5791214019718605909</id><published>2009-02-15T21:21:00.001+02:00</published><updated>2009-02-15T21:21:14.885+02:00</updated><title type='text'>Some more C# 3.0 features</title><content type='html'>&lt;p&gt;In VFP we have eventhandler and bindevents that does binding of methods to the methods of an object we define.&amp;#160; Likewise in C# there are delegates. Delegates are types (remember classes and structs are called types – delegate is also a type, as well as interfaces). Here is the definition of delegate from C# Programming Guide: &lt;/p&gt;  &lt;blockquote&gt;   &lt;p align="justify"&gt;A delegate is a type that defines a method signature, and can be associated with any method with a compatible signature. You can invoke (or call) the method through the delegate. Delegates are used to pass methods as arguments to other methods. Event handlers are nothing more than methods that are invoked through delegates. You create a custom method and a class such as a windows control can call your method when a certain event occurs.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;You will find a lot of samples about delegates but I think it is one of the things that is hard for VFP coders to understand (because in VFP it looks like as if it is a user defined function call but it is not).&amp;#160; Here is a sample:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;using System;      &lt;br /&gt;using System.Collections.Generic;       &lt;br /&gt;using System.Linq;       &lt;br /&gt;using System.Text; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;namespace DelegateSampling      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public delegate string MyNameFormatter(string first, string last);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public delegate double MyCustomMath(int[] examScores); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; public class Student      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public Student(string f, string l, int[] scores)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.FirstName = f;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.LastName = l;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.Scores = scores;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public string FirstName { get; set; }      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public string LastName { get; set; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public int[] Scores { get; set; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; class Sample      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; static void Main(string[] args)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; List&amp;lt;Student&amp;gt; pupils = new List&amp;lt;Student&amp;gt;();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; pupils.Add(new Student(&amp;quot;John&amp;quot;, &amp;quot;Doe&amp;quot;, new int[] { 80, 90, 90 }));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; pupils.Add(new Student(&amp;quot;Frank&amp;quot;, &amp;quot;Smith&amp;quot;, new int[] { 70, 80, 85 }));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; pupils.Add(new Student(&amp;quot;Chris&amp;quot;, &amp;quot;Brown&amp;quot;, new int[] { 70, 60, 75 }));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; pupils.Add(new Student(&amp;quot;Tim&amp;quot;, &amp;quot;Fisher&amp;quot;, new int[] { 80, 90, 100 })); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Sample test = new Sample();      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; MyNameFormatter name = FullName;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; MyCustomMath avg = AvgScore;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; MyCustomMath minSc = MinScore;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; MyCustomMath maxSc = MaxScore; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; foreach (Student s in pupils)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&amp;quot;{0} - Avg: {1:F2}, Min: {2:F2} Max:{3:F2}&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; name(s.FirstName, s.LastName),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; avg(s.Scores),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; minSc(s.Scores),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; maxSc(s.Scores));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //define methods that have the same signature as our delegates &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Name formatter      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public static string FullName(string fName, string lName)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return String.Format(&amp;quot;{0} {1}&amp;quot;, fName, lName);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Custom math on scores      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public static double AvgScore(int[] scores)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int total = 0;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; for (int i = 0; i &amp;lt; scores.Length; i++)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; total += scores[i];       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return total / (double)scores.Length;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public static double MinScore(int[] scores)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; double min = (double)((scores.Length == 0) ? 0 : scores[0]);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; for (int i = 0; i &amp;lt; scores.Length; i++)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; min = (min &amp;lt; scores[i]) ? min : (double)scores[i];       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return min;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public static double MaxScore(int[] scores)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; double max = (double)((scores.Length == 0) ? 0 : scores[0]);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; for (int i = 0; i &amp;lt; scores.Length; i++)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; max = (max &amp;gt; scores[i]) ? max : (double)scores[i];       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return max;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;You should be asking what is special about it? You could simply directly call the&amp;#160; methods instead of calling them “through the delegates”. Well you could do that but think about it as if it was a VFP bindevent() call. You could write the method code beforehand and call those methods. However, you are either binding to methods that you don’t have direct access to modify or want to write the code later separated from the original source. Now check this one which does a similar thing as the above – especially check the new MyFullName delegate and the two new different ways of implementation:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;using System;      &lt;br /&gt;using System.Collections.Generic;       &lt;br /&gt;using System.Text; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;namespace DelegateSampling      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public delegate double MyCustomMath(int[] examScores);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public delegate string MyFullName(Student s); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; public class Student      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public Student(string f, string l, int[] scores)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.FirstName = f;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.LastName = l;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.Scores = scores;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public string FirstName { get; set; }      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public string LastName { get; set; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public int[] Scores { get; set; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; class Sample      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; static void Main(string[] args)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; List&amp;lt;Student&amp;gt; pupils = new List&amp;lt;Student&amp;gt;();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; pupils.Add(new Student(&amp;quot;John&amp;quot;, &amp;quot;Doe&amp;quot;, new int[] { 80, 90, 90 }));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; pupils.Add(new Student(&amp;quot;Frank&amp;quot;, &amp;quot;Smith&amp;quot;, new int[] { 70, 80, 85 }));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; pupils.Add(new Student(&amp;quot;Chris&amp;quot;, &amp;quot;Brown&amp;quot;, new int[] { 70, 60, 75 }));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; pupils.Add(new Student(&amp;quot;Tim&amp;quot;, &amp;quot;Fisher&amp;quot;, new int[] { 80, 90, 100 })); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#000080"&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;font color="#008000"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // inline method implementation - anonymous method            &lt;br /&gt;&lt;/font&gt;&lt;font color="#800000"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; MyFullName lastFirst = delegate(Student s)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&amp;#160; return String.Format(&amp;quot;{0}, {1}&amp;quot;, s.LastName, s.FirstName); }; &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#000080"&gt;&lt;font face="Courier New" size="2"&gt;&amp;#160;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;font color="#008000"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // inline method implementation - anonymous method            &lt;br /&gt;&lt;/font&gt;&lt;font color="#800000"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; MyCustomMath avg = delegate(int[] scores)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int total = 0;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; for (int i = 0; i &amp;lt; scores.Length; i++)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; total += scores[i];             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return total / (double)scores.Length;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }; &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#000080"&gt;&lt;font face="Courier New" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;font color="#008000"&gt;// inline method implementation - lambda expression            &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;strong&gt;&lt;font color="#800080"&gt;MyFullName firstLast = oStudent =&amp;gt; String.Format(&amp;quot;{0} {1}&amp;quot;, oStudent.FirstName, oStudent.LastName); &lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; foreach (Student s in pupils)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&amp;quot;{0} is {1} - Avg: {2:F2}&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; lastFirst(s),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; firstLast(s),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; avg(s.Scores));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;It gets interesting now. We have some inline “method block”s within a single method! Those are called “&lt;a href="http://msdn.microsoft.com/en-us/library/0yw3tz5k(VS.80).aspx" target="_blank"&gt;Anonymous Methods&lt;/a&gt;” which were introduced in C# 2.0. In C# 3.0 another one came in, the “&lt;a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx" target="_blank"&gt;Lambda Expressions&lt;/a&gt;”. &lt;/p&gt;  &lt;p align="justify"&gt;&lt;em&gt;Lambda expressions&lt;/em&gt; are still anonymous methods providing a more compact and functional syntax to implement methods inline. In our sample MyFullName firstLast we have a Lambda Expression that takes a single input parameter of type ‘Student’ and returns a string combining first and last names of given student (input and parameter types are defined as Student and string in delegate definition – remember delegate is a method signature). In our sample we get a single parameter. Suppose instead we would get 2 parameters, a Student and an integer representing a desired scores count, and test if student got enough exams. It would look like:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;public delegate bool MyFilter(Student s, int i);      &lt;br /&gt;// …       &lt;br /&gt;MyFilter f = (s,i) =&amp;gt; s.scores.Length &amp;gt;= i ;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Can you feel the power! Now like in math we have some sort of f(x,y) to be used as a filter function. With generics -we wouldn’t always get (Student, integer) and return a boolean- a function delegate that takes two parametes as inputs and returns a value could be defined as: &lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;public delegate TResult Func&amp;lt;Tinput1,Tinput2,TResult&amp;gt;(TInput1 p1, TInput2 p2);&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;for single input and result it would look like:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;public delegate TResult Func&amp;lt;Tinput1,TResult&amp;gt;(TInput1 p1);&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Since we would need such delegates often C# has already defined delegates that take up to 4 parameters as input and return a value (all named “Func”). That liberates us from declaring a delegate first. IOW you could remove &lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;public delegate string MyFullName(Student s);&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;and slightly change calling codes to:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#800000" size="2"&gt;Func&amp;lt;Student,string&amp;gt; lastFirst = delegate(Student s)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&amp;#160; return String.Format(&amp;quot;{0}, {1}&amp;quot;, s.LastName, s.FirstName); }; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color="#800080"&gt;Func&amp;lt;Student,string&amp;gt; firstLast = oStudent =&amp;gt; String.Format(&amp;quot;{0} {1}&amp;quot;, oStudent.FirstName, oStudent.LastName); &lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;Great, isn’t it :) Removing delegate declarations, writing our inplementations as lambda expressions, refactoring and arranging placement of classes here is our new version of the above code (removed 2 different ways of fullname implemenation), Code also demonstate “statement lambda” which use a statement block to return the average of scores:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;using System;      &lt;br /&gt;using System.Collections.Generic;       &lt;br /&gt;using System.Text; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;namespace DelegateSampling      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; class Sample       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; static void Main(string[] args)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; List&amp;lt;Student&amp;gt; pupils = GetPupils(); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&lt;font color="#008000"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // inline method implementation - lambda expression with statement lambda&lt;/font&gt;       &lt;br /&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Func&amp;lt;int[], double&amp;gt; avg =        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; scores =&amp;gt;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int total = 0;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; for (int i = 0; i &amp;lt; scores.Length; i++)         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; total += scores[i];         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return total / (double)scores.Length;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }; &lt;/font&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#000080"&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;font color="#008000"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // inline method implementation - lambda expression            &lt;br /&gt;&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Func&amp;lt;Student,string&amp;gt; firstLast =            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; oStudent =&amp;gt; String.Format(&amp;quot;{0} {1}&amp;quot;, oStudent.FirstName, oStudent.LastName); &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; foreach (Student s in pupils)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&amp;quot;{0} \t- Avg: {1:F2}&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; firstLast(s),       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; avg(s.Scores));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; private static List&amp;lt;Student&amp;gt; GetPupils()      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; List&amp;lt;Student&amp;gt; pupils = new List&amp;lt;Student&amp;gt;();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; pupils.Add(new Student(&amp;quot;John&amp;quot;, &amp;quot;Doe&amp;quot;, new int[] { 80, 90, 90 }));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; pupils.Add(new Student(&amp;quot;Frank&amp;quot;, &amp;quot;Smith&amp;quot;, new int[] { 70, 80, 85 }));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; pupils.Add(new Student(&amp;quot;Chris&amp;quot;, &amp;quot;Brown&amp;quot;, new int[] { 70, 60, 75 }));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; pupils.Add(new Student(&amp;quot;Tim&amp;quot;, &amp;quot;Fisher&amp;quot;, new int[] { 80, 90, 100 }));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return pupils;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; public class Student      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public Student(string f, string l, int[] scores)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.FirstName = f;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.LastName = l;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.Scores = scores;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public string FirstName { get; set; }      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public string LastName { get; set; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public int[] Scores { get; set; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;Are we done yet? We are nearly done but not yet. Wouldn’t it be nice if we could “extend” the classes and have some methods already defined in them? Lets extend our averaging method and use that instead:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;using System;      &lt;br /&gt;using System.Collections.Generic;       &lt;br /&gt;using System.Text; &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;namespace DelegateSampling      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; using MyExtensions;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; class Sample       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; static void Main(string[] args)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; List&amp;lt;Student&amp;gt; pupils = GetPupils(); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // inline method implementation - lambda expression      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Func&amp;lt;Student,string&amp;gt; firstLast =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; oStudent =&amp;gt; String.Format(&amp;quot;{0} {1}&amp;quot;, oStudent.FirstName, oStudent.LastName); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; foreach (Student s in pupils)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&amp;quot;{0} \t- Avg: {1:F2}&amp;quot;,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; firstLast(s),       &lt;br /&gt;&amp;#160; &lt;font color="#800080"&gt;&lt;strong&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; s.Scores.Average());          &lt;br /&gt;&lt;/strong&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; private static List&amp;lt;Student&amp;gt; GetPupils()      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; List&amp;lt;Student&amp;gt; pupils = new List&amp;lt;Student&amp;gt;();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; pupils.Add(new Student(&amp;quot;John&amp;quot;, &amp;quot;Doe&amp;quot;, new int[] { 80, 90, 90 }));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; pupils.Add(new Student(&amp;quot;Frank&amp;quot;, &amp;quot;Smith&amp;quot;, new int[] { 70, 80, 85 }));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; pupils.Add(new Student(&amp;quot;Chris&amp;quot;, &amp;quot;Brown&amp;quot;, new int[] { 70, 60, 75 }));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; pupils.Add(new Student(&amp;quot;Tim&amp;quot;, &amp;quot;Fisher&amp;quot;, new int[] { 80, 90, 100 }));       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return pupils;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; public class Student      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public Student(string f, string l, int[] scores)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.FirstName = f;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.LastName = l;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.Scores = scores;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public string FirstName { get; set; }      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public string LastName { get; set; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public int[] Scores { get; set; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public List&amp;lt;string&amp;gt; Statistics { get; set; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;} &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;namespace MyExtensions      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public static class Extensions       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public static double Average(this int[] source)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; double total = 0;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; for (int i = 0; i &amp;lt; source.Length; i++)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; total += source[i];       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return total / source.Length;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;}&lt;/font&gt; &lt;/p&gt;  &lt;p align="justify"&gt;We have a namespace with a single method that extends the behaviour of integer arrays (for simplicity integer arrays instead of generic numeric enumerables). It is new to C# 3.0 and means that any integer array (using the namespace we wrote) would have an Average() method defined! DelegateSampling namespace imports “MyExtensions” and we removed “avg” lambda implementation. Instead now we simply do “s.Scores.Average()” –s.Scores is an int[] and we extended integer array sources’ behaviour-. Not surprisingly, the System.Linq space already have an extension method named Avg to average series (and other aggregates like Min,Max,Sum … and even Aggregate which lets you create your own aggregation). &lt;/p&gt;  &lt;p align="justify"&gt;In VFP one of the features that existed from day 0 of foxbase, and what developers love, is the automatic inference of types from declaration. ie:&lt;/p&gt;  &lt;p align="justify"&gt;ix = 3    &lt;br /&gt;name = “cetin”&lt;/p&gt;  &lt;p align="justify"&gt;and ask VFP the types of those variables, it answers as N and C respectively. At least it bothered me why would I need to explicitly type everything even when it is obvious what it is (remember 1.GetType() demostrating it knows the type since C# 1.0?).&amp;#160; Anyway starting with 3.0 C# have the capability infering the type from usage. Now you use the keyword “var”, ie:&lt;/p&gt;  &lt;p align="justify"&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;var i = 3;      &lt;br /&gt;var name = “cetin”;&lt;/font&gt;&lt;/p&gt;  &lt;p align="justify"&gt;And most importantly it is the same as writing:&lt;/p&gt;  &lt;p align="justify"&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;int i = 3;      &lt;br /&gt;string name = “cetin”;&lt;/font&gt;&lt;/p&gt;  &lt;p align="justify"&gt;You can also initialize arrays like this:&lt;/p&gt;  &lt;p align="justify"&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;var numbers = new [] {1,2,3};      &lt;br /&gt;var family = new [] {“ulku”,”efecan”,”cetin”};&lt;/font&gt;&lt;/p&gt;  &lt;p align="justify"&gt;provided you use the variables declared this way only locally. This is called &lt;a href="http://msdn.microsoft.com/en-us/library/bb384061.aspx" target="_blank"&gt;&amp;quot;Implicitly Typed Variables&amp;quot;&lt;/a&gt;.&amp;#160; No doubt implicitly typed variables are cool but there is more.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;a href="ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/dv_csref/html/c58f3db5-d7d4-4651-bd2d-5a3a97357f61.htm" target="_blank"&gt;&amp;quot;Object and Collection Initialzers&amp;quot;&lt;/a&gt; make intializing of record like structures and their collections much easier. We could rewrite our Student class as:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;public class Student      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string FirstName { get; set; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string LastName { get; set; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public int[] Scores { get; set; }       &lt;br /&gt;}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;and then initialize a single student like this (&lt;strong&gt;object initializer&lt;/strong&gt;):&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;new Student { FirstName = &amp;quot;John&amp;quot;,&amp;#160; LastName = &amp;quot;Doe&amp;quot;,&amp;#160;&amp;#160;&amp;#160; Scores = new int[] { 80, 90, 90 }};&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;or alter our GetPupils() method like to initialize a list of students like this (&lt;strong&gt;collection initializer&lt;/strong&gt;):&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;private static List&amp;lt;Student&amp;gt; GetPupils()      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; List&amp;lt;Student&amp;gt; pupils = new List&amp;lt;Student&amp;gt; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; new Student { FirstName = &amp;quot;John&amp;quot;,&amp;#160; LastName = &amp;quot;Doe&amp;quot;,&amp;#160;&amp;#160;&amp;#160; Scores = new int[] { 80, 90, 90 }},       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; new Student { FirstName = &amp;quot;Frank&amp;quot;, LastName = &amp;quot;Smith&amp;quot;,&amp;#160; Scores = new int[] { 70, 80, 85 }},       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; new Student { FirstName = &amp;quot;Chris&amp;quot;, LastName = &amp;quot;Brown&amp;quot;,&amp;#160; Scores = new int[] { 70, 60, 75 }},       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; new Student { FirstName = &amp;quot;Tim&amp;quot;,&amp;#160;&amp;#160; LastName = &amp;quot;Fisher&amp;quot;, Scores = new int[] { 80, 90, 100 }}       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; };       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; return pupils;       &lt;br /&gt;} &lt;/font&gt;&lt;/p&gt;  &lt;p align="justify"&gt;Another feature is similar to inline creation of methods (&lt;strong&gt;Anonymous Methods&lt;/strong&gt;) is the abillity of creating inline types and it is referred to as &lt;a href="http://msdn.microsoft.com/en-us/library/bb397696.aspx" target="_blank"&gt;&amp;quot;Anonymous Types&amp;quot;&lt;/a&gt;. ie:&lt;/p&gt;  &lt;p align="justify"&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;var employee1 = new { Name = “John Doe”, Title = ”Sales Representative”, DepatmentId = 3 };      &lt;br /&gt;var employee2 = new { Name = “Frank Smith”, Title = ”Sales Representative”, DepatmentId = 2 };       &lt;br /&gt;var employee3 = new { Title = ”Sales Representative”, DepatmentId = 3, Name = “Joe Demo” };&lt;/font&gt;&lt;/p&gt;  &lt;p align="justify"&gt;Here we create new type(s) w/o declaring a specific type for them. employee1 is a new type created inline having properties named Name,Title and DepatmentId. employee2 have exactly the same signature and is of the same type of employee1. employee3 have the same number properties and their names are same but the order is different, it would be another type (C# automatically names those types as something like: &lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#008000" size="2"&gt;&amp;lt;&amp;gt;f__AnonymousType0`3[System.String,System.String,System.Int32]      &lt;br /&gt;&amp;lt;&amp;gt;f__AnonymousType0`3[System.String,System.String,System.Int32]       &lt;br /&gt;&amp;lt;&amp;gt;f__AnonymousType1`3[System.String,System.Int32,System.String]&lt;/font&gt; &lt;/p&gt;  &lt;p align="justify"&gt;There are some other new cool features in C# 3.0 (and some more coming up with 4.0) but we are now armed with enough to talk about Linq. There is one important feature about Linq left and that is &lt;a href="http://msdn.microsoft.com/en-us/library/bb310804.aspx" target="_blank"&gt;&amp;quot;Query Keywords&amp;quot;&lt;/a&gt; which VFP developers are familiar with in general. I don’t list them here because starting with the next post we are diving into Linq samples:) In those samples we will see tons of them plus we would see how using lambda expressions is not scary but may be more effective. &lt;/p&gt;  &lt;p align="justify"&gt;Since we are going to work on Linq starting from next post please be sure you have downloaded one of the coolest utilities available, the &lt;a href="http://www.linqpad.net" target="_blank"&gt;LinqPad&lt;/a&gt;. The LinqPad is for free if you don’t enable auto completion. I am not a salesman for Joseph Albahari and actually he doesn’t know me at all, but I strongly suggest that you also activate autocompletion. Neverthless to say I have started to read his book too on ur university Safari subscription and would be one of the books I truely suggest. (I have VFP versions, VS, SQL server and of course LinqPad on my quick launch bar to give an idea how much I respect that).&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-5791214019718605909?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/5791214019718605909/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=5791214019718605909&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/5791214019718605909'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/5791214019718605909'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/02/some-more-c-30-features.html' title='Some more C# 3.0 features'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-3184333338552293520</id><published>2009-02-12T04:23:00.001+02:00</published><updated>2009-02-12T04:23:11.676+02:00</updated><title type='text'>A bit generic</title><content type='html'>&lt;p&gt;C# 2.0 introduced a series of new features and 3.0 added a bunch of others that together make up the infrastructure needed for Linq. We start to explore them now.&lt;/p&gt;  &lt;p&gt;You already learned that C# is a strongly typed language and System.Object is the base of all types. We also talked about the generics a bit. Look at this VFP code:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;procedure FindMinimum( tuParameter1, tuParameter2 )      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; * code checking if they can be compared       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if m.tuParameter1 &amp;lt; m.tuParameter2       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; luResult = m.tuParameter1       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; else       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; luResult = m.tuParameter2       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; endif       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; return m.luResult       &lt;br /&gt;endproc&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;You can call a code like this with different types:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;FindMinimum(“a”,”b”)      &lt;br /&gt;FindMinimum(1,2)       &lt;br /&gt;FindMinimum( {^2008/1/1}, {^2007/1/1} )&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;and VFP would be getting 2 values of the same type and returning minimum of those 2 with the same type as the parameters (assuming there weren’t any errors like passing a date and a boolean). Send 2 strings and get back a string, 2 numbers and get back a number and so on. In C# it is not that easy. You have to declare the types:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;public int FindMinimum(int p1, int p2)      &lt;br /&gt;{       &lt;br /&gt;if (p1 &amp;lt; p2)       &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160; return p1;       &lt;br /&gt;}       &lt;br /&gt;else       &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160; return p2       &lt;br /&gt;}       &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;A C# coder would unlikely write it as verbose as I did (to be clear on what we are doing) and could write it much shorter like:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;public int FindMinimum(int p1, int p2) {&amp;#160; return&amp;#160; (p1 &amp;lt; p2)? p1:p2; }&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;However it would work for only integers. You can add others:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;public string FindMinimum(string p1, string p2) {&amp;#160; return&amp;#160; (p1 &amp;lt; p2)? p1:p2; }      &lt;br /&gt;public double FindMinimum(double p1, double p2) {&amp;#160; return&amp;#160; (p1 &amp;lt; p2)? p1:p2; }       &lt;br /&gt;public DateTime FindMinimum(DateTime p1, DateTime p2) {&amp;#160; return&amp;#160; (p1 &amp;lt; p2)? p1:p2; }       &lt;br /&gt;// ....&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Did you notice that now we have a series of FindMinimum() method codes? Name is same (C# is a case sensitive language, FindMinimum() and findMinimum() are two different methods – it is not recommended to differ methods by casing) but the types are different and that makes the “&lt;em&gt;method signature&lt;/em&gt;”s different (a method’s access level and modifiers –here public-, return value, method name and method parameters together constitute a &lt;strong&gt;Method Signature&lt;/strong&gt; ).&amp;#160; You can find the details &lt;a href="http://msdn.microsoft.com/en-us/library/ms173114.aspx" target="_blank"&gt;here&lt;/a&gt;. If you make a call like FindMinimum(1,2) then C# calls the version defined as FindMinimum(int p1, int p2). Calling part is OK but would you write all those methods for a series of types? You may think that you are clever and instead write this version:&lt;/p&gt;  &lt;p&gt;&lt;font color="#000080"&gt;&lt;font face="Courier New" size="2"&gt;public object FindMinimum(object p1, object p2)        &lt;br /&gt;{         &lt;br /&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;font color="#008000"&gt;&amp;#160;&amp;#160; // code to check if objects implement IComparable            &lt;br /&gt;&amp;#160;&amp;#160; // code to check underlying type, if type can be converted etc             &lt;br /&gt;&amp;#160;&amp;#160; // comparison using the IComparable implementation and return the result&lt;/font&gt;&amp;#160;&amp;#160; &lt;br /&gt;}&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;I didn’t even attempt to write that complex code here! Explicit versions do not even need extra coding in them. As you can see trying to be clever we couldn’t even write a simple “generic” version of the code (maybe we could if we have insisted to write that way but then it would be scary to test such a code). Assuming we were able to do it and wrote the “generic” version, that would be a poorly performing piece of an application (think what if we need such a comparison to sort thousands of items – generally such a comparison code target sorting and or finding the smallest/largest in a set). Did somebody say generic? Oh yeah C# generics. Let’s rewrite it:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;public T FindMinimum&amp;lt;T&amp;gt;(T p1, T p2) where T : IComparable&amp;lt;T&amp;gt;      &lt;br /&gt;{ return ( p1.CompareTo( p2 ) == -1 )?p1:p2; }&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;It is not the best maybe but it is still a cool generic implementation. Here T is a &lt;em&gt;placeholder&lt;/em&gt; for the actual types. We have also used a constraint on that type ( where T : IComparable&amp;lt;T&amp;gt; ) and filtered the types to be only those that implement IComparable interface. Interfaces are contracts with no implementation and forces the type that use it to implement. We don’t have something in VFP to compare with “interface” (or maybe there is and I was not aware of it all these years – all I know it had it for activex interface implementation, as in eventhandler code). Interface is just a class without any implementation of its methods. Practically speaking, you define the methods and document what should it get and return (with a definition of what that return value mean) but do not write the body. Any class that “inherit” that interface must explicitly implement all of its methods. IComparable&amp;lt;T&amp;gt; interface has a single CompareTo(T) member function that accepts an object of type T and returns an integer (0 if this = T, –1 if this &amp;lt; T and 1 if this &amp;gt; T – this as VFP’s this to mean this instance).&amp;#160; We call our new &lt;em&gt;FindMinimum&amp;lt;T&amp;gt;(T p1, T p2)&lt;/em&gt; like this:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New" color="#000080" size="2"&gt;int iResult = FindMinimum&amp;lt;int&amp;gt;(1,2);      &lt;br /&gt;string sResult = FindMinimum&amp;lt;string&amp;gt;(“a”,”b”);       &lt;br /&gt;DateTime dResult = FindMinimum&amp;lt;DateTime&amp;gt;( new DateTime(2008,1,1), new DateTime(2003,1,1) );&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Generic types methods are different from the previous example (or attempt) of creating a generalized version. With “generalized” one there are a lot of things going on, at least up/down casting of the types from/to object (an operation called boxing/unboxing is done behind the scenes, don’t get confused with it but know that it is an expensive operation). With “generic” implementation we are writing a single FindMinimum() but we do help the compiler by explicitly telling what the type(s) would be in the calls we use and compiler creates multiple versions of the FindMinimum() for those types (in our sample compiler would create 3&amp;#160; FindMinimum() with int, string and DateTime types).&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;You can check generics fundementals FAQ &lt;a href="http://msdn.microsoft.com/en-us/library/aa479859.aspx" target="_blank"&gt;here&lt;/a&gt; and the details of generics &lt;a href="http://msdn.microsoft.com/en-us/library/512aeb7t.aspx" target="_blank"&gt;here.&lt;/a&gt; We will continue with these features in next post.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-3184333338552293520?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/3184333338552293520/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=3184333338552293520&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/3184333338552293520'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/3184333338552293520'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/02/bit-generic.html' title='A bit generic'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-7317822740163783831</id><published>2009-02-10T03:59:00.001+02:00</published><updated>2009-02-10T03:59:25.990+02:00</updated><title type='text'>Collections of columns, rows, classes make up the data</title><content type='html'>&lt;p&gt;Last time we were talking about types and ended with a fictitious Customer type. In general that is how you work with data in .Net. A row become a type (remember we call Class and Struct &amp;quot;type”) where fields are its properties and a collection of those objects constitute a “table”. We akso talked about arrays but not collections. Collection classes are numerous in .Net. You can see the classes in System.Collections &lt;a href="http://msdn.microsoft.com/en-us/library/system.collections.aspx" target="_blank"&gt;here&lt;/a&gt; and each have some specific duty. Within the collection classes most widely known and used one is the ArrayList class. You can think of an ArrayList as an ‘array’ or collection in VFP where you can add/remove new “object”s. ArrayList sounds to be a good flexible collection class that holds whatever you throw at it. If you look its members they are “object”s or to say it otherwise System.Object instances. That is very similar to VFP arrays where you are not enforced to use the same type for its members. That is not good from a performance point but more importantly you would have trouble using such a class to store your structured data (C# is a strongly typed language and compiler checks your types for consistency, however in the case of “object” type the compiler functionality would diminish. You could have an ArrayList for Customer type with no guarantee that an Employee type wouldn’t sneak in). &lt;/p&gt;  &lt;p&gt;.Net generics that came with 2.0 was a solution to “typed” collections (generics is not only about collections). You can see System.Collection.Generic namespace members &lt;a href="http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx" target="_blank"&gt;here&lt;/a&gt;.&amp;#160; The advantage of generic collections is that you specify what the type of its members would be. ie:&lt;/p&gt;  &lt;p&gt;List&amp;lt;string&amp;gt; names = new List&amp;lt;string&amp;gt;();    &lt;br /&gt;names.Add(“cetin”);     &lt;br /&gt;names.Add(“joe”);     &lt;br /&gt;List&amp;lt;Customer&amp;gt; customers = new List&amp;lt;Customer&amp;gt;();     &lt;br /&gt;Customer X = new Customer(); // with constructor parameters if any     &lt;br /&gt;Customer Y = new Customer(); // with constructor parameters if any     &lt;br /&gt;X.customerID = “MSFT”;     &lt;br /&gt;Y.customerID = “ORCL”;&amp;#160; &lt;br /&gt;customers.Add( X );     &lt;br /&gt;customers.Add( Y );&lt;/p&gt;  &lt;p&gt;You can’t add say an integer or Customer to “names” list. You can’t add a string to “customers” list. Generic lists have some usefull methods that are not found in non-generic collections. Arrays and Collections are &lt;em&gt;sequences&lt;/em&gt; and &lt;em&gt;sequences&lt;/em&gt; are an important part of data operations (anywhere it is the case, in VFP you can think of the sequence as a group of records, in SQL server a set of data and so on – well like saying the same thing with different sentences). In the future we will see tons of code related to collections and some specifics about them as needed. But for now we need to know another aspect of data in .Net, the core “data” processing classes.&lt;/p&gt;  &lt;p&gt;In the heart of data management with .Net there is (or there were from an aspect) ADO.Net and its classes . Doing an analogy with VFP in a gross scale, a database is represented with “DataSet” class, table is represented with “DataTable” and relations with “DataRelation”.&amp;#160; There are multiple tables and multiple relations, DataTableCollection and DataRelationCollection classes host them in the DataSet. Within a DataTable records are represented wih DataRowCollection (collection of DataRow class), columns with DataColumnCollection (collection of DataColumn class) and ConstraintCollection (collection of Constraint class). Each of these are classes and have their own set of PEM. Developers used typed and untyped datasets within ADO.Net. Untyped sets were those that do not have predefined classes of its tables, columns and so on but generically created at runtime (as if you do a browse with a table that user chooses at runtime – browsing is easy but you can only do few generic operations on such data). Typed datasets are generated by VS tools from existing data such as SQL server database. You generally generate and do not touch the generated classes (they are like scx, vcx … files if you modify them outside designer they would be overwritten). You can find books dedicated solely to ADO.Net and its classes and how to do data management with ADO.Net in .Net. I won’t go into ADO.Net details but give some samples later. The reason I am not dealing with its details, while it is in the heart of .Net data operations, is because now it has already evolved and C# 3.0 and .Net framework 3.5 made it somewhat obsolete (sorry if obsolete is not the right word, what I mean you may do perfectly valid and effective data processing and yet don’t even be aware of that you used one those ADO.Net classes). On the other hand, it may be unfair skipping the painfull story of .Net data processing before Linq, I don’t know. Anyway we would see how it goes when we get into Linq and coding for data.&lt;/p&gt;  &lt;p&gt;(A side note, this might be a llittle strange but if you check the books about C#.Net you would see that in all those books the space allocated to working with data is less than 1/10th. However it is the Linq that encourages me to talk about data processing in .Net. Since Linq came out you can find books that just talks about data processing with Linq ).&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-7317822740163783831?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/7317822740163783831/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=7317822740163783831&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/7317822740163783831'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/7317822740163783831'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/02/collections-of-columns-rows-classes.html' title='Collections of columns, rows, classes make up the data'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-9154287775409833327</id><published>2009-02-07T05:34:00.001+02:00</published><updated>2009-02-07T05:34:54.706+02:00</updated><title type='text'>Typing is important</title><content type='html'>&lt;p align="justify"&gt;Let’s start with some terminology (I will however explicitly skip some details that I think there is no practical importance to a beginner and wouldn’t be much loss if you never learn). &lt;/p&gt;  &lt;p align="justify"&gt;In VFP we have a some “type”s like C for character, N for numeric, D for Date …&amp;#160; and O for object. We generally don’t realize that some of these are complex or compund types with the exception of object. To use all are simple types and we call all classes as of type “O - Object”.&lt;/p&gt;  &lt;p align="justify"&gt;In .Net, remember, everything is an object. All those objects’ class are TYPEs -rather than simply saying it is an object a type refers to its ‘class’ is (however read on though saying class here it doesn’t need to be a class but a struct, you would see shortly). To give some samples, a Type can be Int32, String, DateTime, DataGrid, DataSet, DataTable, Form, IEnumerable, ISerializable … In other words, in .Net you would see Type a lot and it refers to a class or struct ( it is very important to know what an ‘object’ type is and not surprisingly the base class of all objects “System.Object” have a method GetType – remember 1.GetType() ? And also there is a System.Type class).&amp;#160; &lt;/p&gt;  &lt;p align="justify"&gt;Types in a gross scale are classified into Value and Reference types ( we are not saying Class but Type ). Value types are “Structure” and Reference types are “Class”. There are a few slight differences between Structure and Class, if I tell them all you would get confused, here is what matters for you in a practical but unoffical way:&lt;/p&gt;  &lt;p align="justify"&gt;In VFP you declare a class named “test” like this (In VFP we need to specify a base class type, just to fullfill that let’s say this one is custom):&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;define class test as custom      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; SomeProperty = “An initial value”       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; AnotherProperty = 1       &lt;br /&gt;      &lt;br /&gt;&amp;#160;&amp;#160; Procedure init       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * blah blah       &lt;br /&gt;endproc       &lt;br /&gt;enddefine&lt;/p&gt; &lt;/blockquote&gt;  &lt;p align="justify"&gt;In .Net if it is a Class (similar to VFP’s protected,public,hidden in .Net access modifiers exists -private,public,internal,protected- and used often but not used in this code for simplicity – and in fact “property” in this sample is a misnomer):&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;class Test      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; string SomeProperty = “An initial value”;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int AnotherProperty = 1;&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public Test()      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // blah blah       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public int SomeMethod() { // … }       &lt;br /&gt;}&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;was possible. If it were a structure it was not,because:&lt;/p&gt;  &lt;ul&gt;&lt;l  I&gt;A structure cannot initialize its “field”s at their point of declaration. &lt;l  I&gt;A structure cannot have a “default constructor” ( a default constructor is a parameterless constructor –what we know as init method- ) &lt;l  I&gt;A structure must initialize all of its fields in its constructor methods.&lt;/ul&gt;  &lt;p&gt;and it would look like this instead: &lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;struct Test      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public Test( string someProperty, int another )       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.SomeProperty = someProperty;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.AnotherProperty = another;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public int SomeMethod() { // … }       &lt;br /&gt;}&lt;/p&gt; &lt;/blockquote&gt;  &lt;p align="justify"&gt;Note that there is no method named “init” there. A method that has the same name as the class/struct is the constructor –init- method. A constructor doesn’t have a return type either.&lt;/p&gt;  &lt;p align="justify"&gt;Also note that SomeProperty and AnotherProperty are not properties (as we know in VFP) but “fields” of the type ( once again to remind, type means class or struct ). A “Field” is what we know as a “property”.&amp;#160; A “property” on the other hand have get and/or set methods (like access and assign methods). However, you are lucky in 3.0 and later you don’t need the declaration of a field and then implementation of a property with get/set – of course you can if you want to. Now it’s like how it is in VFP and simply looks like:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;public string MyProperty {get;set;} &lt;/p&gt; &lt;/blockquote&gt;  &lt;p align="justify"&gt;Structures are Value types and typical samples are int (an alias for System.Int32 structure), DateTime, Boolean, Void, Nullable(T) …&amp;#160; - all built-in C# types are aliases to System.X where X is a structure (except string, which is a class). You would often use such types: int, uint, byte, object, bool … for example int is an alias to System.Int32 while byte, short, long are aliases to System.Byte, System.Int16,&amp;#160; and System.Int64 structures respectively (signed).&lt;/p&gt;  &lt;p&gt;Classes are Reference types and mst of the types in the .Net are class, such as string (alias to System.String class), DataTable, Array, Type …&lt;/p&gt;  &lt;p align="justify"&gt;When passing them around, as you should have already guessed from VFP’s by value/by reference, value types are passed as copies, while reference types are passed as pointers to object. Value types live on the stack and just like in VFP when a “value type” variable goes of scope it is freed from stack (a good anology of stack is plates in a restaurant, new dish gets on top and the last dish is always the first to leave ‘stack’). Class types (reference IOW) on the other hand live on a location called heap and when an “object” goes out of scope the space it allocates in memory isn’t immediately released. A special garbage collection system is always on duty and reclaims the memory but need not be immediately. How this effect you? You shuld of course live w/o knowing this differences but it is always good to know especially when it may effect performance. Simple lightweight types are good for Structures, there is little to copy on stack when passing them around. If it is a large object then either having it as a class or passing by reference (ref and out keywords exists to do that) is the documented recommendation (and for purists passing by reference is a security matter).&lt;/p&gt;  &lt;p&gt;A row from Customer table can be thought as an object derived from a “Customer” class. Fields of table then become the properties of Customer. ie:&lt;/p&gt;  &lt;p&gt;public class Customer    &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string CustomerId {get; set;}     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string CompanyName {get; set;}     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string ContactName {get;set;}     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public DateTime? LastUpdated {get; set;} // ? stands for nullable &lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; // since this is a class it can have PEM    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // such as other fields and properties that are not data columns     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // calculations, events &lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; // just to honor sampling consider ContactName exists as a column    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // but there is no ContactFirst, ContactLast columns ( or imagine employee First,Last exist but not FullName ) &lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; // for simplicity no error check and assumed single word firstName like “Joe Doe”    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string ContactFirst {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; get { return ContactName.Trim().Split( new char[] { ' ' } )[0]; }&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; set { ContactName = ContactName.Replace( ContactName.Trim().Split( new char[] { ' ' } )[0], value ); } } &lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public string ContactLast {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; get { return ContactName.Trim().Split( new char[] { ' ' } )[1]; }&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; set { ContactName = ContactName.Replace( ContactName.Trim().Split( new char[] { ' ' } )[1], value ); } } &lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public double Balance()    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // ….     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // return …     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //List&amp;lt;Order&amp;gt; Orders = ...     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public double OrdersTotal()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //&amp;#160;&amp;#160; return this.Orders.Sum( o =&amp;gt; o.Quantity * o.Price );     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;} &lt;/p&gt;  &lt;p align="justify"&gt;Thinking a row as an object is not something we are accustomed to in VFP. The languge is object oriented but relational databases are not suited well to think in object terms. I think however you can see (with the knowledge of classes and access/assign methods from VFP) the power of looking a row as an object. It can have navigational “graph”&amp;#160; to its child rows and parent rows ( child Orders or example ), have customizable methods directly on the class etc. A collection of this Customer type would constitute a “Customer Table”. This would then lead to new queries that we are not accustomed to yet but much more eaiser and powerfull sometimes. Such type of queies could be called OQL queries (Object Query Language). &lt;/p&gt;  &lt;p&gt;Before closing today’s post I would to add that Customer above is a user defined “type”. You would create a “new” Customer like this:&lt;/p&gt;  &lt;p&gt;Customer c = new Customer(); // like scatter name or append/inert into buffered table    &lt;br /&gt;c.CustomerId = “CSHARP”; // like replace     &lt;br /&gt;//…     &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-9154287775409833327?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/9154287775409833327/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=9154287775409833327&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/9154287775409833327'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/9154287775409833327'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/02/typing-is-important.html' title='Typing is important'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-8667970867506409270</id><published>2009-02-04T23:33:00.000+02:00</published><updated>2009-02-05T04:17:40.331+02:00</updated><title type='text'>Can you count it?</title><content type='html'>Today before we go on let me clarify a point with a sample code. I got a feedback claiming that you can't do things like:&lt;br /&gt;&lt;br /&gt;1.ToString() or 1.GetType() in C#. I accept that it might be confusing to attach a method to a literal like 1 but you can do it. You can even do that with 1U, 1F, 1.0,1m etc. U, F,m are type specifier suffix (U means unsigned integer, F float, D double, m Decimal). Here is a sample code at the risk of confusing more:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;using System;&lt;br /&gt;&lt;br /&gt;class YouCanDoThatIfYouBelieve&lt;br /&gt;{&lt;br /&gt;   static void Main()&lt;br /&gt;   {&lt;br /&gt;     Console.WriteLine( 1.ToString() );&lt;br /&gt;     Console.WriteLine( 1.GetType().ToString() );&lt;br /&gt;     Console.WriteLine( 1.GetType().GetField("MinValue").GetRawConstantValue().ToString() );&lt;br /&gt;     Console.WriteLine( 1.GetType().GetField("MaxValue").GetRawConstantValue().ToString() );&lt;br /&gt;&lt;br /&gt;     Console.WriteLine(&lt;br /&gt;        1U.GetType().GetField("MinValue").GetRawConstantValue().ToString() );&lt;br /&gt;     Console.WriteLine(&lt;br /&gt;        1U.GetType().GetField("MaxValue").GetRawConstantValue().ToString() );&lt;br /&gt;&lt;br /&gt;     Console.WriteLine( 1.0.GetType().ToString() );&lt;br /&gt;     Console.WriteLine( 1.0.GetType().ToString() );&lt;br /&gt;&lt;br /&gt;     Console.WriteLine(&lt;br /&gt;        1F.GetType().GetField("MinValue").GetRawConstantValue().ToString() );&lt;br /&gt;     Console.WriteLine(&lt;br /&gt;        1F.GetType().GetField("MaxValue").GetRawConstantValue().ToString() );&lt;br /&gt;&lt;br /&gt;     Console.WriteLine(&lt;br /&gt;        1D.GetType().GetField("MinValue").GetRawConstantValue().ToString() );&lt;br /&gt;     Console.WriteLine(&lt;br /&gt;        1D.GetType().GetField("MaxValue").GetRawConstantValue().ToString() );&lt;br /&gt;&lt;br /&gt;     Console.WriteLine( 1m.GetType().ToString() );&lt;br /&gt;     Console.WriteLine( 1m.GetType().ToString() );&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Though it works like this probably you should believe in "you can't do that" phrase and go with the verbose mode like:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;int number = 1;&lt;br /&gt;string myString = number.ToString();&lt;/pre&gt;&lt;hr /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Back on track. Today I want to talk about Arrays (and maybe more if time and space permits). &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;Arrays in C# is on steroids (squared steroids) compared to VFP arrays. You can create single or multi dimensional arrays (multi dimensional means multiple dimensions really -in VFP we can only use up to 2 dimensions- but managing multiple dimension arrays should be hard).&lt;br /&gt;Here are some differences from VFP array first:&lt;br /&gt;All elements of a C# array are of the same type.&lt;br /&gt;The size is specified during declaration (either implicitly or explicitly).&lt;br /&gt;Arrays are zero based (first element starts at 0 unlike 1 in VFP)&lt;br /&gt;An array could have 0 elements (yet another confusion, but don't worry)&lt;br /&gt;&lt;br /&gt;This code from MSDN library shows different types of arrays and how to initialize them:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;// Single-dimensional array (numbers).&lt;br /&gt;int[] n1 = new int[4] {2, 4, 6, 8};&lt;br /&gt;int[] n2 = new int[] {2, 4, 6, 8};&lt;br /&gt;int[] n3 = {2, 4, 6, 8};&lt;br /&gt;&lt;br /&gt;// Single-dimensional array (strings).&lt;br /&gt;string[] s1 = new string[3] {"John", "Paul", "Mary"};&lt;br /&gt;string[] s2 = new string[] {"John", "Paul", "Mary"};&lt;br /&gt;string[] s3 = {"John", "Paul", "Mary"};&lt;br /&gt;&lt;br /&gt;// Multidimensional array.&lt;br /&gt;int[,] n4 = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };&lt;br /&gt;int[,] n5 = new int[,] { {1, 2}, {3, 4}, {5, 6} };&lt;br /&gt;int[,] n6 = { {1, 2}, {3, 4}, {5, 6} };&lt;br /&gt;&lt;br /&gt;// Jagged array.&lt;br /&gt;int[][] n7 = new int[2][] { new int[] {2,4,6}, new int[] {1,3,5,7,9} };&lt;br /&gt;int[][] n8 = new int[][] { new int[] {2,4,6}, new int[] {1,3,5,7,9} };&lt;br /&gt;int[][] n9 = { new int[] {2,4,6}, new int[] {1,3,5,7,9} };&lt;/pre&gt;&lt;br /&gt;In the sample above, jagged arrays initialization make it a little confusing. If you look closely it is like array of arrays where each array member can have different Length (element count) arrays - n9 have 2 'arrays' one with 3 elements and the other 5.&lt;br /&gt;On the other hand, two dimensional array shown as a sample for multidimension arrays, are referred to as "rectangular arrays" - they are like VFP's 2D arrays with fixed size rows and columns.&lt;br /&gt;You can see how easy it is to initialize an array let's see a tip (something that we can't do in VFP):&lt;br /&gt;&lt;br /&gt;You can split a string into tokens using String class' Split() method (think of alines() in VFP). One overload of Split() expects an array for chars. You could do this:&lt;br /&gt;&lt;pre&gt;string someString = "One, two, three and four. Parse into tokens.";&lt;br /&gt;char[] delimiters = new char[] {',','.',' '};&lt;br /&gt;string[] tokens = someString.Split(delimiters)&lt;/pre&gt;Nothing special here but look at this one doing the same thing:&lt;br /&gt;&lt;pre&gt;string someString = "One, two, three and four. Parse into tokens.";&lt;br /&gt;string[] tokens = someString.Split(new char[] {',','.',' '});&lt;/pre&gt;The ability to declare and initialize an array at the location where it is expected as a parameter is cool compared to VFP IMHO (using new ... where a parameter is expected is not limited to arrays). Among C# developers this is a very simple thing that they wouldn't think of it as a 'tip' but I thought this detail would escape from many VFP developers (since we don't expect a capability like that). Related with this let me add this:&lt;br /&gt;A string is an array of chars and string class has an "indexer" to its chars (an indexer is a special type of method that accepts a parameter(s) and returns one of its members - like grid.Columns[i], excelSpreadSheet.Cells(1,1), integer.bits[5], collection.Item["key"]) you can do this:&lt;br /&gt;&lt;pre&gt;string test= "My String";&lt;br /&gt;char myChar = test[1]&lt;/pre&gt;Keeping in mind that C# is zero based, this would return 'y' as a char (a char is a unicode value and this might not be the correct way to get the 2nd character of a string but in practice it works like this). In the same vein since a string is an array of chars you can create a new string from an array of chars by simply creating a string object (did I ever say new is like VFP's createobject() and used to create an object) passing the array to its 'constructor' method (constructor is what we know as Init).&lt;br /&gt;&lt;pre&gt;char[] charArray = {'a','b','c'};&lt;br /&gt;string concatted = new string(charArray);&lt;br /&gt;// concatted is now a string - "abc"&lt;br /&gt;&lt;/pre&gt;I hope you are seeing the power behind this seemingly very simple (for .Net) things. &lt;br /&gt;&lt;br /&gt;Yes, of course I didn't show anything that you can't do in VFP but it is much easier in .Net, isn't it?&lt;br /&gt;&lt;br /&gt; (BTW I told you that array is initialized with a known size - that is true but you can use "Array" class and Array.Resize( myArray, newSize )  - it is not an efficient operation because since an array have a predefined size, this method does the resizing by creating a new one and then replacing with the old)&lt;br /&gt;&lt;br /&gt;Arrays are important in any language and in .Net it is someting that you can 'count' its elements. If you can count it then it means you can iterate its elements or in other words you can enumerate it as if you are giving record numbers and that should ring some bells:) In VFP we could do SQL:&lt;br /&gt;&lt;br /&gt;select ... from ... where ...     &lt;br /&gt;&lt;br /&gt;and the 'Query' source is a table (or anything like that, a cursor). C# has gained "Language INtegrated Query" aka LINQ  capability, it can do "SQL" now (well not SQL but SQLlike). Interestingly though the source need not be a table, it can be anything that you can get an enumerator on or to say it otherwise anything you can count:)&lt;br /&gt;&lt;br /&gt;Hopefully we will complete some more stuff this week and then "Let the fun begin" with Linq (I really became an addict of Linq and F# I think but who wants to have medication for it:) Just to keep your appetite alive, here are samples to some 'countable' things (no need to confuse you with IEnumerable, I... yet) that you can do "Query" operations on:&lt;br /&gt;&lt;br /&gt;Arrays, strings, any type collections (for example directories and files, running applications and processes), XML documents, any text file (it has lines, characters etc that you can count) ... and why not, data files:)&lt;br /&gt;&lt;br /&gt;Do you remember this?&lt;pre&gt;var q = "Count distinct characters in this string".Distinct();&lt;/pre&gt;If should now make a little more sense. &lt;br /&gt;&lt;br /&gt;See you on next post.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-8667970867506409270?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/8667970867506409270/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=8667970867506409270&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/8667970867506409270'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/8667970867506409270'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/02/can-you-count-it.html' title='Can you count it?'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-7797539020961925755</id><published>2009-02-04T04:25:00.001+02:00</published><updated>2009-02-04T16:47:59.181+02:00</updated><title type='text'>Welcome back.</title><content type='html'>&lt;p&gt;Yesterday we made a head start to .Net bypassing all the basics and going directly into Linq (what C# 3.0 was about). Afterall, at least for sometime, this blog series target VFP developers who already have an idea about basic programming and curious about how would they do the data centric operations that they do everyday. Today however I think we should go into some basics at least about the syntax and a few more. &lt;/p&gt;&lt;p&gt;While reading, please keep in mind I prefer not to use a formal language and strict formal definitions. That bores me in the the first place and wouldn’t help to keep the reader awake :) If you are purist then you can read the official &lt;a href="http://msdn.microsoft.com/en-us/vcsharp/aa336809.aspx" target="_blank"&gt;C# Language Specification&lt;/a&gt;. Instead I prefer a relaxed “toy with it” approach. &lt;/p&gt;&lt;p&gt;You have probably heard it already, everything in .Net is an object. Huh? Everything? Yes, everything. What does everything mean then? Just what it says, literally anything. In VFP:&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;number&lt;/strong&gt; = &lt;strong&gt;1&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;oForm&lt;/strong&gt; = createobject(&lt;strong&gt;“form”&lt;/strong&gt;)&lt;/p&gt;&lt;br /&gt;&lt;p&gt;there is only one object (object variable) in this code and that is ‘oForm’. Now for a second think of it were a C# or VB code (assuming the syntax was right). Then there would be 4 objects shown (those are in bold in the code). How can that be? Here it goes, you can find it playing a QA game:&lt;/p&gt;&lt;p&gt;What is 1? A number. Right, but can you classify it a bit more? An integer. Very true:) So 1 is an “object” subclassed from “integer class”.&lt;br /&gt;What is “form”? A string. Right on target, ”form” is an “object” from “string class”.&lt;/p&gt;&lt;p&gt;number and oForm are object variables of integer and form ‘type’ respectively (but later you would see that number and oForm should have their class –referred to as TYPE in .Net- specified in C#). &lt;/p&gt;&lt;p&gt;If they are objects then they should have something like a method and yes they have. All objects’ base class is ‘object class’ and to be more precise System.Object class. Object Class have some methods like ToString(), GetHashCode() … and interestingly you can use these methods on 1 and “form”.&lt;/p&gt;&lt;p&gt;1.ToString()&lt;br /&gt;1.GetType()&lt;br /&gt;”form”.GetHashCode()&lt;/p&gt;&lt;p&gt;since “form” is a string instance we could use a string method like this too:&lt;/p&gt;&lt;br /&gt;&lt;p&gt;“form”.ToUpper()&lt;/p&gt;&lt;br /&gt;&lt;p&gt;OK I assume you are now convinced that they are objects (all TYPEs –read it classes to understand- are derived from object and this is referred to as Common Type System, aka CTS). &lt;/p&gt;&lt;p&gt;Notice that ToString() and GetHashCode() are “attached”. In VFP if we needed to convert a variable named &lt;em&gt;luVar&lt;/em&gt; to a string, get its substr(…,2,3), then uppercase it and trim:&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;alltrim( upper( substr( transform( luVar ),2,3 ) ) )&lt;/span&gt;&lt;/p&gt;&lt;p&gt;luVar might be luVar variable or 1, “form” etc and in C# we do:&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;luVar.ToString().Substring(1,3).ToUpper().Trim()&lt;/span&gt;&lt;/p&gt;&lt;p&gt;since method names are explanatory you can see the pattern, methods are attached and chained. You should also note something important here for C# – C# is a case sensitive language and counting starts from 0:&lt;/p&gt;&lt;br /&gt;&lt;p&gt;ToString is the right method name, you can’t write it like Tostring, toString, tostring…&lt;br /&gt;substr( “astring”, 2, 3) or anArray[2] in VFP means start from 2nd character or 2nd member respectively.&lt;br /&gt;In C# it means 3rd (0 based). Thus a typical for loop in VFP looks like:&lt;/p&gt;&lt;pre&gt;for i = 1 to 10&lt;/pre&gt;while in C# it is:&lt;br /&gt;&lt;pre&gt;for(int i=0; i &amp;lt; 10; i++)&lt;/pre&gt;&lt;p&gt;OK now lets continue. In VFP consider we have a code like:&lt;pre&gt;&lt;span style="color:#000099;"&gt;Procedure SomeProcedure( tcString As String, tnStart As Int, tnEnd As Int) As String&lt;br /&gt;Local ix As Int, result As String&lt;br /&gt;result = ""&lt;br /&gt;For ix = m.tnStart To m.tnEnd&lt;br /&gt;  result = &amp;lt; Something &amp;gt;&lt;br /&gt;  local jx as int&lt;br /&gt;  jx = …&lt;br /&gt;Endfor&lt;br /&gt;result = &amp;lt; Some more operation here &amp;gt;&lt;br /&gt;if ( result = &amp;lt; Something &amp;gt; )&lt;br /&gt;  * …&lt;br /&gt;Endif&lt;br /&gt;TEXT to lcText noshow&lt;br /&gt;I want these lines to appear as I wrote.&lt;br /&gt;Line 2&lt;br /&gt;Line 3&lt;br /&gt;ENDTEXT&lt;br /&gt;* …&lt;br /&gt;result = Textmerge("On Date: &amp;lt;&amp;lt; Date() &amp;gt;&amp;gt;:") + Chr(13)+Chr(10)+;&lt;br /&gt;         m.result + ;&lt;br /&gt;         " Some Text "&lt;br /&gt;Return m.result&lt;br /&gt;Endproc&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color:#000000;"&gt;In C# syntax:&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color:#000099;"&gt;string SomeProcedure( string tcString, int tnStart, int tnEnd)&lt;br /&gt;{&lt;br /&gt;   string result = "";&lt;br /&gt;   for( int ix = m.tnStart; ix &amp;lt; m.tnEnd; ix++ )&lt;br /&gt;   {&lt;br /&gt;      result = &amp;lt; Something &amp;gt; ;&lt;br /&gt;      int jx = …&lt;br /&gt;   }&lt;br /&gt;   result = &amp;lt; Something &amp;gt; ;&lt;br /&gt;   if ( result == &amp;lt; Something &amp;gt; )&lt;br /&gt;   {&lt;br /&gt;      // …&lt;br /&gt;   }&lt;br /&gt;   string lcText = @"I want these lines to appear as I wrote.&lt;br /&gt;Line 2&lt;br /&gt;Line 3"&lt;br /&gt;&lt;br /&gt;// …&lt;br /&gt;&lt;br /&gt;result = String.Format(“On Date: {0}:{1}”,DateTime.Today.ToString(“d”),Environment.NewLine) +&lt;br /&gt;    result +&lt;br /&gt;    " Some Text " ;&lt;br /&gt;&lt;br /&gt;Return result;&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;Note the differences:&lt;br /&gt;&lt;table border="1" cellspacing="0" cellpadding="2" width="90%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td valign="top" width="316"&gt;&lt;p align="center"&gt;&lt;strong&gt;&lt;span style="font-size:100%;"&gt;VFP&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;td valign="top" width="347"&gt;&lt;p align="center"&gt;&lt;strong&gt;&lt;span style="font-size:100%;"&gt;C#&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td valign="top" width="316"&gt;Parameter, return and variable types are optional Type of variables are not specified before they are used (specification is only for intellisense support when you use)&lt;/td&gt;&lt;td valign="top" width="347"&gt;Parameter, return and variable types are mandatory&lt;br /&gt;TYPE (class) of variables are specified before they are used (strongly typed language)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td valign="top" width="316"&gt;There are different block specifiers like procedure …endproc, for … endfor&lt;/td&gt;&lt;td valign="top" width="347"&gt;Code blocks are specified with curly braces { }.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td valign="top" width="316"&gt;Local variable declaration makes the variable to be scoped to the whole procedure from the point it is declared.&lt;/td&gt;&lt;td valign="top" width="347"&gt;&lt;p&gt;A local variable is scoped to the block it is declared in (practically scoped to its outer curly braces { } ).&lt;/p&gt;&lt;p&gt;For example: In the code above both ix and jx variables are scoped to the for loop. If later in the code you were to use ix you need to decalre it.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td valign="top" width="316"&gt;&lt;/td&gt;&lt;td valign="top" width="347"&gt;@” … “ construct is a special version of “ … ” that treates the string like text…endtext of VFP&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td valign="top" width="316"&gt;Equality operator (=) is both used to assign and test equality&lt;/td&gt;&lt;td valign="top" width="347"&gt;= (single) is used for assignment and == (double) is used for testing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td valign="top" width="316"&gt;[ENTER] ends a command line&lt;br /&gt;Semicolon is used to continue on next line&lt;/td&gt;&lt;td valign="top" width="347"&gt;Semicolon ends a statement (command line)&lt;br /&gt;[ENTER] is used to continue on next line&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;To recap, continuation example:&lt;pre&gt;&lt;span style="color:#000099;"&gt;string result = aStringVariable.ToString().Substring(1,3).ToUpper().Trim();&lt;br /&gt;&lt;br /&gt;string result =&lt;br /&gt;     aStringVariable.ToString()&lt;br /&gt;                    .Substring(1,3)&lt;br /&gt;                    .ToUpper()&lt;br /&gt;                    .Trim();&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;are equally valid syntax.&lt;br /&gt;&lt;br /&gt;I think it is enough for today folks, soon we will end the boring parts and have some fun.&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-7797539020961925755?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/7797539020961925755/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=7797539020961925755&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/7797539020961925755'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/7797539020961925755'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/02/welcome-back.html' title='Welcome back.'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2230683952377059025.post-6895091598643233283</id><published>2009-02-03T03:17:00.001+02:00</published><updated>2009-02-03T03:54:57.316+02:00</updated><title type='text'>New Horizons</title><content type='html'>VFP has always been good to me and it is what I use to earn my life.  However it is time to move on to more promising languages. I, like many other VFP developers, started my move little by little to .Net (and chose C#/F# as my .Net language(s)).&lt;br /&gt;&lt;br /&gt;Why would I move? VFP is easy, very good at data processing and  I can do whatever I need with it. Well I wouldn't argue how good VFP is. It is good, right but not as good as to be at work in a few years (mostly because we would see 64 bits OS in use widely - I would say in a few months). Your codes would work or you could continue to create new VFP applications, even after year 2015 as long as you and your customers can live with 32 bits. If you don't see it as a threat, then still there are reasons for you if you answer yes to a few of these questions:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Want to create richer UI? Saying richer, I mean it. A UI that you have never experienced in VFP with WPF. WPF - Windows Presentation Foundation which is a set of APIs with 2/3D graphics engine gives you the ability to create user interfaces where the only limit is your imagination. &lt;/li&gt;&lt;li&gt;Want to create web applications besides desktop applications or a hybrid of them? &lt;/li&gt;&lt;li&gt;Want to create distributed applications?   &lt;/li&gt;&lt;li&gt;Want to create applications for mobile devices? &lt;/li&gt;&lt;li&gt;Want distribution of updates to your software to be controlled by proven technologies w/o requiring you to write code? &lt;/li&gt;&lt;li&gt;Want to parse and/or compose XML documents using classes that knows XML well? &lt;/li&gt;&lt;li&gt;Want to send email, create network connections, check credentials, authenticate logins, ftp etc, in other words do network related operations without a need to use hard to use win32APIs, activex etc tools? &lt;/li&gt;&lt;li&gt;Want to draw charts, text, vector graphics to devices? Again using the classes that are at your disposal w/o win32APIs and activex. &lt;/li&gt;&lt;li&gt;Want to have the ability to write your code and compile it when you are at your customer's site and all you have is a newly installed windows XP or later? &lt;/li&gt;&lt;li&gt;Want to write applications that work on multiple threads, multiple processors, multiple monitors ... multiple platforms? &lt;/li&gt;&lt;/ul&gt;And the list would go on ...  "OK but as a VFP developer I have questions" you say.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Q:&lt;/strong&gt; If I decide to work with .Net which language should I choose? VB or C#?&lt;br /&gt;&lt;strong&gt;A: &lt;/strong&gt;There is no solid answer to that question (I don't have one and haven't read one at any site yet). You may choose any .Net language you want and  .Net languages are not limited to VB and C#. You can see a list of them &lt;a href="http://www.dotnetpowered.com/languages.aspx" target="_blank"&gt;here &lt;/a&gt;and &lt;a href="http://en.wikipedia.org/wiki/Category:.NET_programming_languages" target="_blank"&gt;here&lt;/a&gt;. It is a shame anything I know and heard of (and those I have never heard before) are on that list while VFP is not &lt;img title="Kızgın" style="VERTICAL-ALIGN: middle" alt="Kızgın" src="http://shared.live.com/csi!Rvmz5kFbfVkDrhMJKg/emoticons/smile_angry.gif" /&gt;&lt;br /&gt;I chose C# (and F#). Whatever you choose wouldn't matter much because you would be using .Net classes and (if you take out language specific syntax) most of the code that you write would be same - it is not the case always:) Anyway check documentation page for example. You would see code written in different .Net languages look very similar. Visual Studio 2008 have C#, VB an C++ (2005 also had J#) but it is extensible and different .Net languages have IDE created for them. For C# there are other IDE too like &lt;a href="http://www.icsharpcode.net/OpenSource/SD/" target="_blank"&gt;SharpDevelop&lt;/a&gt;.  In VS2010 F# will be included (I am not sure about IronPython and IronRuby).&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Q:&lt;/strong&gt; I work with VFP because its language is easy. What about .Net? Wouldn't I have a steep learning curve?&lt;br /&gt;&lt;strong&gt;A:&lt;/strong&gt; If being easy is a concern, you can think of using VB.Net. Many developers claim that VB is an easy language and targeted to Rapid Application Development RAD. Easy, on the other hand, is a relative term. To me C# is easy (and F# probably easier - don't know if I feel like that because I started it after C# or because I have worked with a similar language years ago).&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Q:&lt;/strong&gt; What is important to me is data processing. Is that easy and fast like it is with VFP?&lt;br /&gt;&lt;strong&gt;A: &lt;/strong&gt;Short answer is yes. On the long run it is even better. During .Net 1.1 days data munging in .Net was awfull (and I have published some demo code both in C# and VFP against SQL server data to demonstrate VFP was exceptionally faster on UniversalThread). However with 2.0 it got faster. Linq (later on this LanguageINtegratedQuery) gave a boost and each and everyday it is getting much better. VFP developers, generally think about local VFP native data. &lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Q:&lt;/strong&gt; But .Net doesn't have a native data backend.&lt;br /&gt;&lt;strong&gt;A:&lt;/strong&gt; Yes, it doesn't. But just like you could use any data sources in VFP you could use them in .Net and doing that in .Net has got easier. In .Net you have a greater range of datasources.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Q:&lt;/strong&gt; What would be the initial cost I need to make while I am learning and have nothing to sell yet?&lt;br /&gt;&lt;strong&gt;A: &lt;/strong&gt;Zero.&lt;br /&gt;&lt;br /&gt;OK if I couldn't sell you .Net today I can do it in the upcoming blog entries. For those that I could, lets see some action:)&lt;br /&gt;&lt;br /&gt;What do you need to start with? Ideal environment is Visual Studio full blown version but you can start with one of the Express versions.&lt;br /&gt;All the tools you need initially have 'Express' versions. 'Express' denote lightweight version of X in .Net world. Besides languages and IDEs, you would find &lt;a href="http://www.microsoft.com/express/default.aspx" target="_blank"&gt;Express versions&lt;/a&gt; of popular databases like SQL server, Oracle. Also check &lt;a href="http://msdn.microsoft.com/en-us/beginner/default.aspx" target="_blank"&gt;beginner developer center&lt;/a&gt;.  Before we move on, I strongly suggest one of the coolest downloads you can find and that is &lt;a href="http://www.linqpad.net/" target="_blank"&gt;LinqPad&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;I assume you chose C# &lt;img title="Çekici" style="VERTICAL-ALIGN: middle" alt="Çekici" src="http://shared.live.com/csi!Rvmz5kFbfVkDrhMJKg/emoticons/smile_shades.gif" /&gt; and have at least C# express. Let's kick off.&lt;br /&gt;&lt;br /&gt;Every programming starts with the famous "Hello World" application. If you want, you can create that masterpiece and watch it for hours later but for now lets do a little more basic. VFP reminds data so lets start with a LINQ sample and simulate a scan ... endscan. Go and launch C# express (or VS if you have it). Before we continue, if you are like me, you would want to create temporary test codes and then either save or discard them. In .Net it is called  &lt;a href="http://msdn.microsoft.com/en-us/library/6yx39k28.aspx" target="_blank"&gt;"Temporary Projects"&lt;/a&gt; and you need to enable it. Go to tools\Options and select "Projects and Solutions" (check Show all settings), be sure "Save new projects when created" is not checked.&lt;br /&gt;&lt;br /&gt;&lt;a href="https://mucvla.bay.livefilestore.com/y1mklVyBAa5nk_NWYoD3hUTJuVElagNVR4VLfSgPVcMGDLLxT6aw7yguTmyS4pyvN09K92Ib-ocwYvQ2aMo8QISDDPvNkExnxI_nJImoJCvBvDPgPAK6EC2vX33aHLVoXVCrcQJC-9fyTs/Options.JPG" target="_blank" rel="WLPP;url="&gt;&lt;img alt="" src="https://mucvla.bay.livefilestore.com/y1mklVyBAa5nk_NWYoD3hUTJuVElagNVR4VLfSgPVcMGDLLxT6aw7yguTmyS4pyvN09K92Ib-ocwYvQ2aMo8QISDDPvNkExnxI_nJImoJCvBvDPgPAK6EC2vX33aHLVoXVCrcQJC-9fyTs/Options.JPG" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;OK we are ready, lets Query &lt;img title="Göz kırpma" style="VERTICAL-ALIGN: middle" alt="Göz kırpma" src="http://shared.live.com/csi!Rvmz5kFbfVkDrhMJKg/emoticons/smile_wink.gif" /&gt; We start with a simple "Console Application". Console Application is what we know as DOS application.&lt;br /&gt;&lt;br /&gt;&lt;a href="https://mucvla.bay.livefilestore.com/y1mqWPeoi0c_WyMY2uh4tw6zyH-rwJlhDQTKF7-yeT2fDPquqQxRmNJdJqHccgKAhglj3C7S2NRxNvgPibLJ3SF6ehkl07XaYHP-pH714-bu75p7DkJGW-Tl8Gih2VkDpGCmMMjrs7lmIA/TempConsoleApp.JPG" target="_blank" rel="WLPP;url="&gt;&lt;img alt="" src="https://mucvla.bay.livefilestore.com/y1mqWPeoi0c_WyMY2uh4tw6zyH-rwJlhDQTKF7-yeT2fDPquqQxRmNJdJqHccgKAhglj3C7S2NRxNvgPibLJ3SF6ehkl07XaYHP-pH714-bu75p7DkJGW-Tl8Gih2VkDpGCmMMjrs7lmIA/TempConsoleApp.JPG" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;If you opt to choose using &lt;a href="http://msdn.microsoft.com/en-us/library/6yx39k28.aspx" target="_blank"&gt;"Temporary Projects"&lt;/a&gt; don't even bother to change its name. Since we directly dived into the heart of .Net w/o the wonderfull "Hello World" and other basics this might be hard to catch at first but don't bother, just do for now. We are simply toying with it:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Go to Data\Add new Datasource and select 'Database'. Add a connection to SQL Server Northwind database and add all tables in it (if you don't have SQL Server you can download express version and if you don't have northwind sample database you can download it from &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=06616212-0356-46A0-8DA2-EEBC53A68034&amp;amp;displaylang=en" target="_blank"&gt;here&lt;/a&gt;). &lt;/li&gt;&lt;li&gt;Right click project and choose "Add new item" &lt;/li&gt;&lt;li&gt;Add Linq To SQL class (since the release of EF Linq To EF supersedes Linq To SQL - &lt;a href="http://blogs.msdn.com/adonet/archive/2008/10/29/update-on-linq-to-sql-and-linq-to-entities-roadmap.aspx" target="_blank"&gt;details here&lt;/a&gt; and you can download Entity Framework &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=FBEE1648-7106-44A7-9649-6D9F6D58056E&amp;amp;displaylang=en" target="_blank"&gt;here&lt;/a&gt;). I won't debate on usefullness of Linq To SQL (referred as L2S) today. IMHO it is still has value, at least in understanding Linq syntax if nothing else.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;a href="https://mucvla.bay.livefilestore.com/y1mhSCZKI5jb4J9euum3AZ7F2ARuBWNhykk6b7p1-NaXhFGFRh5QAGLRmyMYos82CGXz6q_UftaT5whtUx-PuniRkZJNH8-g_kLHWzaZiVv1ST6ccrT3Pkl0wZSWOF8XME5c_NgUujLZtk/LinqToSQL.JPG" target="_blank" rel="WLPP;url="&gt;&lt;img alt="" src="https://mucvla.bay.livefilestore.com/y1mhSCZKI5jb4J9euum3AZ7F2ARuBWNhykk6b7p1-NaXhFGFRh5QAGLRmyMYos82CGXz6q_UftaT5whtUx-PuniRkZJNH8-g_kLHWzaZiVv1ST6ccrT3Pkl0wZSWOF8XME5c_NgUujLZtk/LinqToSQL.JPG" /&gt;&lt;/a&gt; &lt;/p&gt;&lt;p&gt;&lt;a href="https://mucvla.bay.livefilestore.com/y1mq7Xq9DYbyDGR9JLKWhT-wrSuUABmqq9VcAPDCsMO5auIs-FxiB-2WDeg_g-Td2LJ_juAE2YmDLivBtcoZ_9G-iMzcHzsPRtG-6-zgyj28B48jiaowlGVltMo0l0e3upS4pOWY-WzGY8/LinqToSQL2.JPG" target="_blank" rel="WLPP;url="&gt;&lt;img alt="" src="https://mucvla.bay.livefilestore.com/y1mq7Xq9DYbyDGR9JLKWhT-wrSuUABmqq9VcAPDCsMO5auIs-FxiB-2WDeg_g-Td2LJ_juAE2YmDLivBtcoZ_9G-iMzcHzsPRtG-6-zgyj28B48jiaowlGVltMo0l0e3upS4pOWY-WzGY8/LinqToSQL2.JPG" /&gt;&lt;/a&gt;&lt;a href="https://mucvla.bay.livefilestore.com/y1mhSCZKI5jb4J9euum3AZ7F2ARuBWNhykk6b7p1-NaXhFGFRh5QAGLRmyMYos82CGXz6q_UftaT5whtUx-PuniRkZJNH8-g_kLHWzaZiVv1ST6ccrT3Pkl0wZSWOF8XME5c_NgUujLZtk/LinqToSQL.JPG" target="_blank" rel="WLPP;url="&gt;&lt;/a&gt; &lt;/p&gt;Click "Database Explorer", expand Northwind database and drag &amp;amp; drop Customers, Orders, Order Details and Employee tables (and other if you wish) onto the design surface as shown below:&lt;br /&gt;&lt;br /&gt;&lt;a href="https://mucvla.bay.livefilestore.com/y1mOhebvV63VHQJRjZVY7iG88PzRhizEaqvEW241CqRWp1b_N8QFdCBkxG9xXJcRjwEUB6G2b3Vl50UNk24w-axYxeygW2JQn0EkJWxg543Cg6fDuee7TC2AGgEgEzK1LAU7s66XsC3HiA/LinqToSQL3.JPG" target="_blank" rel="WLPP;url="&gt;&lt;img alt="" src="https://mucvla.bay.livefilestore.com/y1mOhebvV63VHQJRjZVY7iG88PzRhizEaqvEW241CqRWp1b_N8QFdCBkxG9xXJcRjwEUB6G2b3Vl50UNk24w-axYxeygW2JQn0EkJWxg543Cg6fDuee7TC2AGgEgEzK1LAU7s66XsC3HiA/LinqToSQL3.JPG" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Note that I started L2S file creation with Nwind.dbml name and it gave the NWindDataContext name to the newly created "DataContext" (DataContext is a class - skip details now).&lt;br /&gt;&lt;br /&gt;OK now double click "Program.cs" and arrnage the code so it reads:&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Text;&lt;br /&gt;&lt;br /&gt;namespace ConsoleApplication1&lt;br /&gt;{&lt;br /&gt;    class Program&lt;br /&gt;    {&lt;br /&gt;        static void Main(string[] args)&lt;br /&gt;        {&lt;br /&gt;            NwindDataContext db = new NwindDataContext();&lt;br /&gt;            var queryCustomers =&lt;br /&gt;                from c in db.Customers&lt;br /&gt;                where c.Orders.Any(o =&amp;gt;&lt;br /&gt;                    o.OrderDate.HasValue &amp;amp;&amp;amp;&lt;br /&gt;                    o.OrderDate.Value.Year == 1997)&lt;br /&gt;                select c;&lt;br /&gt;&lt;br /&gt;            foreach (var cus in queryCustomers)&lt;br /&gt;            {&lt;br /&gt;                Console.WriteLine("Customer {0} made {1} orders. First:{2} Last: {3}",&lt;br /&gt;                    cus.CustomerID,&lt;br /&gt;                    cus.Orders.Count,&lt;br /&gt;                    cus.Orders.Min(o =&amp;gt; o.OrderDate).Value,&lt;br /&gt;                    cus.Orders.Max(o =&amp;gt; o.OrderDate).Value);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Copy &amp;amp; paste the code if you are lazy:) But I strongly suggest you type it so you would see how you get Intellisense support. To run it press [Ctrl+F5] or from menu &lt;br /&gt;"Debug\Start without debugging".&lt;br /&gt;&lt;br /&gt;A very important note here: This query is not meant to be an efficient one. You would see why as you progress and/or monitor what commands are executing against SQL server (for now using SQL profiler) and when.&lt;br /&gt;&lt;br /&gt;Tip: &lt;br /&gt;var queryCustomers =&lt;br /&gt;                from c in db.Customers&lt;br /&gt;                where c.Orders.Any(o =&amp;gt;&lt;br /&gt;                    o.OrderDate.HasValue &amp;amp;&amp;amp;&lt;br /&gt;                    o.OrderDate.Value.Year == 1997)&lt;br /&gt;                select c;&lt;br /&gt;&lt;br /&gt;is like&lt;br /&gt;&lt;br /&gt;text to lcSQL noshow&lt;br /&gt;select ....  where .... =?..&lt;br /&gt;endtext&lt;br /&gt;&lt;br /&gt;in a sense. It is not executed until results are asked in any way like an SQLExec() (here with foreach() which simulate a Scan...EndScan). And just like SQLExec() it is called again against to the backend (but unlike SQLExec() or RV requery() it has some controlling options). It is too much for a start IMHO.  What you should note anayway is that, there is some SQL like code in C# code. That is "LanguageINtegratedQuery" that gets executed uniformly independant of the underlying datasource (in VFP that is CursorAdapter that can come closest but still much far away from it - in fact I shouldn't even try to make a correlation). And folks this is only the tip of the iceberg with Linq! Just for fun following line is also a Linq expressions:&lt;br /&gt;&lt;br /&gt;// Count distinct characters in a  string&lt;br /&gt;&lt;span style="font-family:Courier New;"&gt;var q = "Count distinct characters in this string".Distinct();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Or this one:&lt;br /&gt;// DBF files under VFP home folder and its subfolders&lt;br /&gt;// Note: This one gets simple string list (oh well  array for purists)&lt;br /&gt;// using a slightly different variation we could return an array of "file information" objects&lt;br /&gt;var dbfFiles = &lt;span style="font-family:Consolas;font-size:85%;color:#0000ff;"&gt;&lt;span style="font-family:Consolas;font-size:85%;color:#0000ff;"&gt;&lt;span style="font-family:Consolas;font-size:85%;color:#0000ff;"&gt;&lt;p&gt;   from&lt;/p&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:85%;"&gt;&lt;span style="font-family:Consolas;font-size:85%;color:#000000;"&gt;f in Directory.GetFiles(&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:85%;color:#dc1414;"&gt;&lt;span style="font-family:Consolas;font-size:85%;color:#dc1414;"&gt;&lt;span style="font-family:Consolas;font-size:85%;color:#dc1414;"&gt;@"C:\Program Files\Microsoft Visual FoxPro 9"&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:85%;"&gt;&lt;span style="font-family:Consolas;font-size:85%;color:#000000;"&gt;,&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:85%;color:#dc1414;"&gt;&lt;span style="font-family:Consolas;font-size:85%;color:#dc1414;"&gt;&lt;span style="font-family:Consolas;font-size:85%;color:#dc1414;"&gt;                                 "*.dbf"&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:85%;"&gt;&lt;span style="font-family:Consolas;font-size:85%;color:#000000;"&gt;,&lt;br /&gt;                                 SearchOption.AllDirectories)&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:85%;color:#0000ff;"&gt;&lt;span style="font-family:Consolas;font-size:85%;color:#0000ff;"&gt;&lt;span style="font-family:Consolas;font-size:85%;color:#0000ff;"&gt;   select&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:85%;"&gt;&lt;span style="font-family:Consolas;font-size:85%;"&gt;&lt;span style="color:#000000;"&gt; f;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;p&gt;To kill your curiosity and test some code easier, see extra code generations etc do not forget to download the COOL &lt;a href="http://www.linqpad.net/" target="_blank"&gt;LinqPad&lt;/a&gt;. If you pay few bucks you can activate the autocompetion and of course the book itself is a breeze IMHO (finally my dream came true and now I have ability to reach safari and I can read it - BTW there are literally hundreds and maybe thousands of other good resources). &lt;/p&gt;&lt;p&gt;Oh well moer than enough I think for the first blog entry&lt;img title="Açık ağızlı" style="VERTICAL-ALIGN: middle" alt="Açık ağızlı" src="http://shared.live.com/csi!Rvmz5kFbfVkDrhMJKg/emoticons/smile_teeth.gif" /&gt; Till next time you can check out this video - &lt;a href="http://msdn.microsoft.com/en-us/beginner/bb964631.aspx" target="_blank"&gt;Introduction to C# Express&lt;/a&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2230683952377059025-6895091598643233283?l=cetinbasoz.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cetinbasoz.blogspot.com/feeds/6895091598643233283/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2230683952377059025&amp;postID=6895091598643233283&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/6895091598643233283'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2230683952377059025/posts/default/6895091598643233283'/><link rel='alternate' type='text/html' href='http://cetinbasoz.blogspot.com/2009/02/new-horizons.html' title='New Horizons'/><author><name>cbasoz</name><uri>http://www.blogger.com/profile/18276054387132537049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
