<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Edwin M Sarmiento&gt;LINQ for Beginners &#8211; Edwin M Sarmiento</title>
	<atom:link href="https://www.edwinmsarmiento.com/linq-for-beginners/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.edwinmsarmiento.com</link>
	<description>Intentional Excellence</description>
	<lastBuildDate>Mon, 13 Apr 2026 21:00:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
<site xmlns="com-wordpress:feed-additions:1">84283043</site>		<item>
		<title>&gt;LINQ for Beginners</title>
		<link>https://www.edwinmsarmiento.com/linq-for-beginners/</link>
		<comments>https://www.edwinmsarmiento.com/linq-for-beginners/#comments</comments>
		<pubDate>Mon, 21 Jan 2008 05:21:00 +0000</pubDate>
		<dc:creator>Edwin M Sarmiento</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<guid isPermaLink="false">http://bassplayerdoc.wordpress.com/2008/01/21/linq-for-beginners</guid>

				<description><![CDATA[&#62;Someone asked me about what was on my MSN Messenger status and explained to him what I was doing. Apparently, I was doing LINQ and had it as my MSN status. LINQ stands for Language INtegrated Query, is a condename for a project for a set of extensions to the .NET Framework that encompasses language-integrated [&#8230;]]]></description>
					<content:encoded><![CDATA[<p>&gt;<span style="font-family:arial;">Someone asked me about what was on my MSN Messenger status and explained to him what I was doing. Apparently, I was doing LINQ and had it as my MSN status. LINQ stands for <strong><span style="font-size:85%;">Language INtegrated Query</span></strong>, is a condename for a project for a set of extensions to the .NET Framework that encompasses language-integrated query, set and transform operations. It extends C# and VB with native language syntax for queries and provides class libraries to take advantage of these capabilities, available only in .NET Framework 3.5 (this simply means if you want to write LINQ queries, they have to be using the correct framework). Now, what does that mean to developers? The fact that queries are usually expressed in a specialized query language for different data sources makes it difficult for developers to learn a query language for each data source or data format that they must access. This is what LINQ is all about. It simplifies data access by providing a consistent model for working with data across various kinds of sources and formats. In LINQ, data is translated into objects, something that developers are more comfortable at working with. Understanding LINQ will give us an idea of its capabilities and its benefits (I&#8217;ll save the cons from an enterprise DBA&#8217;s perspective at a future post).</span></p>
<p><span style="font-family:Arial;">To understand LINQ, we need to know the basic parts of a query operation; namely, obtaining the data source, creating the query and executing the query.</span> <span style="font-family:arial;">This is simply generic &#8211; any access to a data source will definitely have to do these steps.</span></p>
<p><strong><span style="font-size:85%;"><span style="color:blue;">class</span> LINQBasics<br />{<br /><span style="color:blue;">static</span> <span style="color:blue;">void</span> Main()<br />{</p>
<p><span style="color:green;">//Obtaining the data source</span><br /><span style="color:blue;">string</span>[] names = {&#8220;Charlie&#8221;, &#8220;Joe&#8221;, &#8220;Yia Wei&#8221; , &#8220;Bob&#8221;, &#8220;Mike&#8221;};</p>
<p><span style="color:green;">// Create the query</span></span></strong><br /><strong><span style="font-size:85%;"><span style="color:green;">// query is an </span><span style="color:#009900;">IEnumerable </span><br /><span style="color:#000099;">var</span> query = <span style="color:#000099;">from</span> name <span style="color:blue;">in</span> names<br /><span style="color:#000099;">where</span> name.Contains(&#8220;i&#8221;) </span></strong><br /><strong><span style="font-size:85%;"><span style="color:#000099;">orderby</span> name<br /><span style="color:#000099;">selec</span><span style="color:#000099;">t</span> name;</p>
<p><span style="color:green;">// Execute the query</span></span></strong><br /><strong><span style="font-size:85%;"><span style="color:blue;">foreach</span> (<span style="color:blue;">string</span> name <span style="color:blue;">in</span> query)<br />{<br /><span style="color:#009900;">Console.</span>Write(<span style="color:maroon;">name</span>);<br />}<br />}<br />}</span></strong><br /><strong><span style="font-size:85%;"></span></strong><br /><span style="font-family:arial;">Looking at the code above, the first thing that we need to do is to have a data source. In this case, it&#8217;s an array of string which supports the generic <a href="http://msdn2.microsoft.com/en-us/library/9eekhta0.aspx">IEnumerable(T)</a> interface. This makes it available for LINQ to query. A queryable type does not require special modification to serve as a LINQ data source so long as it is already loaded in memory or else you would have to load it into memory so LINQ can query the objects. This is applicable to data sources like XML files. Next, is the query. A query specifies information to retrieve from the data source. If you are familiar with SQL, you know what this looks like &#8211; the kind which includes <strong><span style="font-size:85%;">select</span></strong>, <strong><span style="font-size:85%;">from</span></strong>, <strong><span style="font-size:85%;">where</span></strong> and the likes. Looking at the code above, you&#8217;ll notice that its not like your typical SQL statement as the <strong><span style="font-size:85%;">from</span></strong> clause appeared before the <strong><span style="font-size:85%;">select</span></strong> clause. There are a couple of reasons for this. One, this adheres to the programming concept of delaring the variable before using it. Also, from the point of view of Visual Studio, this makes it easy to provide the <strong><span style="font-size:85%;">IntelliSense</span></strong> feature using the <strong><span style="font-size:85%;">dot (.) notation</span></strong> as the variable has already been declared and that the framework has already inferred the correct type to the object, thus providing the appropriate properties and methods, making it easy for the developers to write their code. Let&#8217;s look at how the code was constructed. The <strong><span style="font-size:85%;">from</span></strong> clause specifies the data source, in this case, the <strong><span style="font-size:85%;">names</span></strong> collection. The <strong><span style="font-size:85%;">where</span></strong> clause applies the filter, in this case, the list of all elements in the collection containing the letter &#8220;<strong>i</strong>.&#8221; The <strong><span style="font-size:85%;">select</span></strong> clause specifies the type of the returned elements. This means that you can create an instance of the elements in your collection. An example could be creating an instance of an object with fewer attributes. The query variable, <strong><span style="font-size:85%;">query</span></strong>, just stores the information required to produce the results when the query is executed maybe at a later point. Simply defining the query variable does not return any data nor takes any action. The third component of the code above is query execution. Like I said, the query variable does not contain any data but rather simply contains only the query commands. The actual execution of the query is when we iterate over the query variable. There are a couple of ways to do this. One of which is shown above. The use of a <strong><span style="font-size:85%;">foreach</span></strong> statement iterates thru the query variable and execute it as well. This concept is called <strong><span style="font-size:85%;">deferred query execution</span></strong>. This is very much important when dealing with data sources such as highly-transactional database systems as you minimize connecting to the database unless necessary (database connections are additional resources on the database server as well). You can opt to execute the query immediately by using aggregate functions such as <strong><span style="font-size:85%;">Count</span></strong>, <strong><span style="font-size:85%;">Max</span></strong>, <strong><span style="font-size:85%;">Average</span></strong> and <strong><span style="font-size:85%;">First</span></strong> or calling the <strong><span style="font-size:85%;">ToList()</span></strong> or <strong><span style="font-size:85%;">ToArray()</span></strong> methods. Another way is to bind the collection to a data-bound control in either a web or windows form control similar to how we do it in previous versions, specifying the <strong><span style="font-size:85%;">DataSource</span></strong> property of the control to be the query variable and calling the <strong><span style="font-size:85%;">DataBind()</span></strong> method. </span><br /><span style="font-family:Arial;"></span><br /><span style="font-family:Arial;">One other thing to highlight is the use of the keyword <strong><span style="font-size:85%;">var</span></strong>, which is a new keyword introduced in C# 3.0. What this does it it looks at the value assigned to the variable and determines and sets the appropriate one. This concept is called type inference. From the code above, the query variable, <strong><span style="font-size:85%;">query</span></strong>, appears to be an array of string. So the compiler will automatically assume that it is a variable of type <strong><span style="font-size:85%;">IEnumerable</span></strong>. This is helpful if you do not know the variable type during runtime. But this does not mean that any type can be assigned to the variable after the initial assignment &#8211; something like a dynamic type &#8211; since .NET is a strongly typed language platform. This simply means that an object can take on a different type and the compiler can simply handle that. Assigning a different type to an already existing one violates the concept of polymorphism in object-oriented programming. Let&#8217;s say you assign the value <strong><span style="font-size:85%;">12</span></strong> to the query variable, <strong><span style="font-size:85%;">query</span></strong>. This will throw a type conversion exception as the original type of the variable is a string collection. </span><br /><span style="font-family:Arial;"></span><br /><span style="font-family:Arial;">This is just a tip of the iceberg for LINQ. There are a lot of reqources out there for LINQ to SQL, LINQ to XML, LINQ to Objects, LINQ to Entities and LINQ to DataSets. I&#8217;ll try to post more examples to make programming in LINQ a bit more appealing to developers. </span><br /><span style="font-family:Arial;"></span><br /><span style="font-family:Arial;">This article is also posted on the <a href="http://www.mssqltips.com/tip.asp?tip=1486">MSSQLTips.com</a> site</span></p>
<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/92377218009570869-5597578341928269286?l=bassplayerdoc.blogspot.com' alt='' /></div>
]]></content:encoded>
			

		<wfw:commentRss>https://www.edwinmsarmiento.com/linq-for-beginners/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
				<post-id xmlns="com-wordpress:feed-additions:1">69</post-id>	</item>
	</channel>
</rss>