<?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>LINQ Archives - Blog IT</title>
	<atom:link href="https://blogit.create.pt/category/dotnet/linq/feed/" rel="self" type="application/rss+xml" />
	<link>https://blogit.create.pt/category/dotnet/linq/</link>
	<description>Create IT blogger community</description>
	<lastBuildDate>Thu, 10 Jan 2019 14:15:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>
	<item>
		<title>LINQ to XML: How to use it?</title>
		<link>https://blogit.create.pt/antoniovargas/2009/10/18/linq-to-xml-how-to-use-it/</link>
					<comments>https://blogit.create.pt/antoniovargas/2009/10/18/linq-to-xml-how-to-use-it/#respond</comments>
		
		<dc:creator><![CDATA[António Vargas]]></dc:creator>
		<pubDate>Sun, 18 Oct 2009 12:46:00 +0000</pubDate>
				<category><![CDATA[LINQ]]></category>
		<category><![CDATA[.NET]]></category>
		<guid isPermaLink="false">http://blogcreate.azurewebsites.net/antoniovargas/?p=361</guid>

					<description><![CDATA[<p>Frequently, the most of people use XPATH to do xml manipulation. But if you want another way to manipulate XML, you can use LINQ to XML. Supposing that we have the following XML: &#60;?xml version=&#34;1.0&#34; encoding=&#34;utf-8&#34;?&#62; &#60;ArrayOfEmployee xmlns:xsi=&#34;http://www.w3.org/2001/XMLSchema-instance&#34; xmlns:xsd=&#34;http://www.w3.org/2001/XMLSchema&#34;&#62; &#60;Employee Status=&#34;Active&#34;&#62; &#60;FirstName&#62;John&#60;/FirstName&#62; &#60;LastName&#62;Smith&#60;/LastName&#62; &#60;Age&#62;32&#60;/Age&#62; &#60;EmployeeNumber&#62;213&#60;/EmployeeNumber&#62; &#60;/Employee&#62; &#60;Employee Status=&#34;Inactive&#34;&#62; &#60;FirstName&#62;Mike&#60;/FirstName&#62; &#60;LastName&#62;Stuart&#60;/LastName&#62; &#60;Age&#62;24&#60;/Age&#62; &#60;EmployeeNumber&#62;253&#60;/EmployeeNumber&#62; &#60;/Employee&#62; &#60;Employee Status=&#34;Inactive&#34;&#62; [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/antoniovargas/2009/10/18/linq-to-xml-how-to-use-it/">LINQ to XML: How to use it?</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Frequently, the most of people use XPATH to do xml manipulation. But if you want another way to manipulate XML, you can use LINQ to XML.</p>
<p>Supposing that we have the following XML:</p>
<pre style="background-color: #fbfbfb;width: 512px;height: 347px;overflow: auto;border: #cecece 1px solid;padding: 5px"><pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px"><span style="color: #0000ff">&lt;?</span>xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;<span style="color: #0000ff">?&gt;</span>
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">ArrayOfEmployee</span> <span style="color: #ff0000">xmlns</span>:<span style="color: #ff0000">xsi</span>=<span style="color: #0000ff">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span> <span style="color: #ff0000">xmlns</span>:<span style="color: #ff0000">xsd</span>=<span style="color: #0000ff">&quot;http://www.w3.org/2001/XMLSchema&quot;</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">Employee</span> <span style="color: #ff0000">Status</span>=<span style="color: #0000ff">&quot;Active&quot;</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">FirstName</span><span style="color: #0000ff">&gt;</span>John<span style="color: #0000ff">&lt;/</span><span style="color: #800000">FirstName</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">LastName</span><span style="color: #0000ff">&gt;</span>Smith<span style="color: #0000ff">&lt;/</span><span style="color: #800000">LastName</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">Age</span><span style="color: #0000ff">&gt;</span>32<span style="color: #0000ff">&lt;/</span><span style="color: #800000">Age</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">EmployeeNumber</span><span style="color: #0000ff">&gt;</span>213<span style="color: #0000ff">&lt;/</span><span style="color: #800000">EmployeeNumber</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Employee</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">Employee</span> <span style="color: #ff0000">Status</span>=<span style="color: #0000ff">&quot;Inactive&quot;</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">FirstName</span><span style="color: #0000ff">&gt;</span>Mike<span style="color: #0000ff">&lt;/</span><span style="color: #800000">FirstName</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">LastName</span><span style="color: #0000ff">&gt;</span>Stuart<span style="color: #0000ff">&lt;/</span><span style="color: #800000">LastName</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">Age</span><span style="color: #0000ff">&gt;</span>24<span style="color: #0000ff">&lt;/</span><span style="color: #800000">Age</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">EmployeeNumber</span><span style="color: #0000ff">&gt;</span>253<span style="color: #0000ff">&lt;/</span><span style="color: #800000">EmployeeNumber</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Employee</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">Employee</span> <span style="color: #ff0000">Status</span>=<span style="color: #0000ff">&quot;Inactive&quot;</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">FirstName</span><span style="color: #0000ff">&gt;</span>John<span style="color: #0000ff">&lt;/</span><span style="color: #800000">FirstName</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">LastName</span><span style="color: #0000ff">&gt;</span>Charles<span style="color: #0000ff">&lt;/</span><span style="color: #800000">LastName</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">Age</span><span style="color: #0000ff">&gt;</span>53<span style="color: #0000ff">&lt;/</span><span style="color: #800000">Age</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">EmployeeNumber</span><span style="color: #0000ff">&gt;</span>214<span style="color: #0000ff">&lt;/</span><span style="color: #800000">EmployeeNumber</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Employee</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">Employee</span> <span style="color: #ff0000">Status</span>=<span style="color: #0000ff">&quot;Active&quot;</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">FirstName</span><span style="color: #0000ff">&gt;</span>Mary<span style="color: #0000ff">&lt;/</span><span style="color: #800000">FirstName</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">LastName</span><span style="color: #0000ff">&gt;</span>Gomez<span style="color: #0000ff">&lt;/</span><span style="color: #800000">LastName</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">Age</span><span style="color: #0000ff">&gt;</span>37<span style="color: #0000ff">&lt;/</span><span style="color: #800000">Age</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">EmployeeNumber</span><span style="color: #0000ff">&gt;</span>234<span style="color: #0000ff">&lt;/</span><span style="color: #800000">EmployeeNumber</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Employee</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">Employee</span> <span style="color: #ff0000">Status</span>=<span style="color: #0000ff">&quot;Inactive&quot;</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">FirstName</span><span style="color: #0000ff">&gt;</span>Javier<span style="color: #0000ff">&lt;/</span><span style="color: #800000">FirstName</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">LastName</span><span style="color: #0000ff">&gt;</span>Matias<span style="color: #0000ff">&lt;/</span><span style="color: #800000">LastName</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">Age</span><span style="color: #0000ff">&gt;</span>33<span style="color: #0000ff">&lt;/</span><span style="color: #800000">Age</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">EmployeeNumber</span><span style="color: #0000ff">&gt;</span>453<span style="color: #0000ff">&lt;/</span><span style="color: #800000">EmployeeNumber</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Employee</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">ArrayOfEmployee</span><span style="color: #0000ff">&gt;</span></pre>
<p>You can filter an employee with a specified employee number (by Element).</p>
<ul>
<li>Code: 
<pre style="background-color: #fbfbfb;width: 472px;height: 188px;overflow: auto;border: #cecece 1px solid;padding: 5px"><pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">Console.WriteLine(&quot;<span style="color: #8b0000">Employee with employee number 213:</span>&quot;);
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px"></pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">XDocument xDoc = XDocument.Load(&quot;<span style="color: #8b0000">AllEmployes.xml</span>&quot;);
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px"></pre>
<pre style="background-color: #ffff00;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">XElement employeeUser = xDoc.Descendants(&quot;<span style="color: #8b0000">Employee</span>&quot;)
</pre>
<pre style="background-color: #ffff00;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    .Where(e =&gt; e.Element(&quot;<span style="color: #8b0000">EmployeeNumber</span>&quot;).Value.Equals(&quot;<span style="color: #8b0000">213</span>&quot;))
</pre>
<pre style="background-color: #ffff00;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    .Single();
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px"></pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">Console.WriteLine(<span style="color: #0000ff">string</span>.Format(&quot;<span style="color: #8b0000">{0} {1} - {2}</span>&quot;, 
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">                                employeeUser.Element(&quot;<span style="color: #8b0000">FirstName</span>&quot;).Value, 
</pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">                                employeeUser.Element(&quot;<span style="color: #8b0000">LastName</span>&quot;).Value, 
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">                                employeeUser.Element(&quot;<span style="color: #8b0000">EmployeeNumber</span>&quot;).Value));</pre>
</li>
<li>Result: <br /><img decoding="async" src="http://img27.imageshack.us/img27/4904/capture2te.jpg"> </li>
</ul>
<p>You can filter all active employees (by Attribute).</p>
<ul>
<li>Code: 
<pre style="background-color: #fbfbfb;width: 474px;height: 168px;overflow: auto;border: #cecece 1px solid;padding: 5px"><pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">Console.WriteLine(&quot;<span style="color: #8b0000">Get all active employees:</span>&quot;);
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px"></pre>
<pre style="background-color: #ffff00;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">List&lt;XElement&gt; allActiveEmployees = xDoc.Descendants(&quot;<span style="color: #8b0000">Employee</span>&quot;)
</pre>
<pre style="background-color: #ffff00;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    .Where(e =&gt; e.Attribute(&quot;<span style="color: #8b0000">Status</span>&quot;).Value.Equals(&quot;<span style="color: #8b0000">Active</span>&quot;))
</pre>
<pre style="background-color: #ffff00;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    .ToList();
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px"></pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px"><span style="color: #0000ff">foreach</span> (XElement employee <span style="color: #0000ff">in</span> allActiveEmployees)
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">{
</pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">    Console.WriteLine(<span style="color: #0000ff">string</span>.Format(&quot;<span style="color: #8b0000">{0} {1} - {2}</span>&quot;,
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">                                    employee.Element(&quot;<span style="color: #8b0000">FirstName</span>&quot;).Value,
</pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">                                    employee.Element(&quot;<span style="color: #8b0000">LastName</span>&quot;).Value,
</pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">                                    employee.Element(&quot;<span style="color: #8b0000">EmployeeNumber</span>&quot;).Value));
</pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 10px">}</pre>
</li>
<li>Result: <br /><img decoding="async" src="http://img9.imageshack.us/img9/1346/capture3fg.jpg"> </li>
</ul>
<p>The post <a href="https://blogit.create.pt/antoniovargas/2009/10/18/linq-to-xml-how-to-use-it/">LINQ to XML: How to use it?</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/antoniovargas/2009/10/18/linq-to-xml-how-to-use-it/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Cast vs OfType: Which should i use to cast an object’s list?</title>
		<link>https://blogit.create.pt/antoniovargas/2009/09/26/cast-vs-oftype-which-should-i-use-to-cast-an-objects-list/</link>
					<comments>https://blogit.create.pt/antoniovargas/2009/09/26/cast-vs-oftype-which-should-i-use-to-cast-an-objects-list/#respond</comments>
		
		<dc:creator><![CDATA[António Vargas]]></dc:creator>
		<pubDate>Sat, 26 Sep 2009 08:50:00 +0000</pubDate>
				<category><![CDATA[LINQ]]></category>
		<category><![CDATA[.NET]]></category>
		<guid isPermaLink="false">http://blogcreate.azurewebsites.net/antoniovargas/?p=421</guid>

					<description><![CDATA[<p>In this post i will explain what&#8217;s the differences between Cast() and OfType() methods using LINQ, and i will also explain when we should use each method. These methods allow us to cast an object type to another, but when you&#8217;re using the Cast() method and exist some object that isn&#8217;t of the expected class [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/antoniovargas/2009/09/26/cast-vs-oftype-which-should-i-use-to-cast-an-objects-list/">Cast vs OfType: Which should i use to cast an object’s list?</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><font face="Trebuchet MS">In this post i will explain what&rsquo;s the differences between Cast() and OfType() methods using LINQ, and i will also explain when we should use each method.</font> </p>
<p><font face="Trebuchet MS">These methods allow us to cast an object type to another, but when you&rsquo;re using the Cast() method </font><font face="Trebuchet MS">and exist some object that isn&rsquo;t of the expected class type, it will throw an exception. If you use the OfType() method, the LINQ query will ignore this objects and it doesn&rsquo;t throw an exception.</font> </p>
<p><font face="Trebuchet MS"><br /></font></p>
<p><font face="Trebuchet MS">For example, i&rsquo;m assuming the following Class Diagram:</font> </p>
<p>&nbsp;&nbsp;&nbsp; <img decoding="async" src="http://img4.imageshack.us/img4/2296/sampleclassesdiagram.png"></p>
<p>&nbsp;</p>
<p>And we have the following data objects on the Person&rsquo;s list:</p>
<pre><pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 11px"><font size="1">_persons.Add(<span style="color: #0000ff">new</span> Employee { FirstName=&quot;<span style="color: #8b0000">John</span>&quot;, LastName=&quot;<span style="color: #8b0000">Smith</span>&quot;, Age=32, EmployeeNumber=213 });<br></font></pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 11px"><font size="1">_persons.Add(<span style="color: #0000ff">new</span> Employee { FirstName = &quot;<span style="color: #8b0000">Mike</span>&quot;, LastName = &quot;<span style="color: #8b0000">Stuart</span>&quot;, Age = 24, EmployeeNumber = 253 });<br></font></pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 11px"><font size="1">_persons.Add(<span style="color: #0000ff">new</span> Employee { FirstName = &quot;<span style="color: #8b0000">John</span>&quot;, LastName = &quot;<span style="color: #8b0000">Charles</span>&quot;, Age = 53, EmployeeNumber = 214 });<br></font></pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 11px"><font size="1">_persons.Add(<span style="color: #0000ff">new</span> Employee { FirstName = &quot;<span style="color: #8b0000">Mary</span>&quot;, LastName = &quot;<span style="color: #8b0000">Gomez</span>&quot;, Age = 37, EmployeeNumber = 234 });<br></font></pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 11px"><font size="1">_persons.Add(<span style="color: #0000ff">new</span> Employee { FirstName = &quot;<span style="color: #8b0000">Javier</span>&quot;, LastName = &quot;<span style="color: #8b0000">Matias</span>&quot;, Age = 33, EmployeeNumber = 453 });<br></font></pre>
<pre style="background-color: #ffffff;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 11px"><font size="1">_persons.Add(<span style="color: #0000ff">new</span> Customer { FirstName = &quot;<span style="color: #8b0000">Bill</span>&quot;, LastName = &quot;<span style="color: #8b0000">Smith</span>&quot;, Age = 32, Type = CustomerType.Financial });<br></font></pre>
<pre style="background-color: #fbfbfb;margin: 0em;width: 100%;font-family: consolas,'Courier New',courier,monospace;font-size: 11px"><font size="1">_persons.Add(<span style="color: #0000ff">new</span> Customer { FirstName = &quot;<span style="color: #8b0000">Jefferson</span>&quot;, LastName = &quot;<span style="color: #8b0000">Orton</span>&quot;, Age = 32, Type = CustomerType.Retail });<br></font></pre>
<pre style="background-color: #ffffff;margin: 0em;width: 63.96%;font-family: consolas,'Courier New',courier,monospace;height: 13px;font-size: 11px"><font size="1">_persons.Add(<span style="color: #0000ff">new</span> Customer { FirstName = &quot;<span style="color: #8b0000">Tom</span>&quot;, LastName = &quot;<span style="color: #8b0000">Mowbray</span>&quot;, Age = 32, Type = CustomerType.Retail });</font></pre>
<p><font face="Trebuchet MS"><br /></font></p>
<p><font face="Trebuchet MS">Using <strong>Cast()</strong> to Get all employees that are on the Person&rsquo;s list</font> </p>
<ul>
<li><font face="Trebuchet MS">LINQ Query:</font> </li>
</ul>
<blockquote>
<pre class="csharpcode"> List&lt;Employee&gt; employees = Persons.Cast&lt;Employee&gt;().ToList();</pre>
</blockquote>
<ul>
<li>
<pre class="csharpcode"><font face="Trebuchet MS">Query Result:</font></pre>
<p><img decoding="async" src="http://img15.imageshack.us/img15/4482/getallemployeeswithcast.png"> </p>
<p><img decoding="async" src="http://img33.imageshack.us/img33/3901/casterror.png"> </li>
</ul>
<p>&nbsp;</p>
<pre class="csharpcode"><font face="Trebuchet MS">Using <strong>OfType()</strong> to Get all employees that are on the Person&rsquo;s list</font></pre>
<ul>
<li>
<pre class="csharpcode"><font face="Trebuchet MS">LINQ Query:</font></pre>
</li>
</ul>
<blockquote>
<pre class="csharpcode">List&lt;Employee&gt; employees = Persons.OfType&lt;Employee&gt;().ToList();</pre>
</blockquote>
<ul>
<li>
<pre class="csharpcode"><font face="Trebuchet MS">Query Result: </font></pre>
<p><img decoding="async" src="http://img190.imageshack.us/img190/5403/getallemployeeswithofty.png"> </li>
</ul>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; </p>
<p>Well, If you need to get an objects with a specified class type from an object&rsquo;s list and you only want this objects you should use the OfType() method. However, If you want to ensure that all objects inside the list are a specified class type, you should use the Cast() method. </p>
<p>The post <a href="https://blogit.create.pt/antoniovargas/2009/09/26/cast-vs-oftype-which-should-i-use-to-cast-an-objects-list/">Cast vs OfType: Which should i use to cast an object’s list?</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/antoniovargas/2009/09/26/cast-vs-oftype-which-should-i-use-to-cast-an-objects-list/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
