<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="2" MadCap:lastHeight="120" MadCap:lastWidth="624" MadCap:disableMasterStylesheet="true" MadCap:tocPath="Basics Operations & Concepts|Querying|LINQ" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem"> <head><title>LINQ Examples</title> <script type="text/javascript">/* <![CDATA[ */ window.onload = function(){ var pathToFlash = $('html').attr('MadCap:PathToHelpSystem') + 'Content/Resources/Code/ZeroClipboard.swf'; ZeroClipboard.setMoviePath(pathToFlash); function bindToClipBord(element,content){ var clip = new ZeroClipboard.Client(); clip.setText(content); clip.glue(element); }; if(location.protocol==='file:'){ $('.copylink-marker').remove(); } else{ $('.copylink-marker').each(function(){ var text = $(this).parent().parent().children('.prettyprint').html(); $(this).hover(function(){ bindToClipBord(this,text); }, function(){}); }); } prettyPrint(); }; /* ]]> */</script> <link href="../../../SkinSupport/MadCap.css" rel="stylesheet" /> <link href="../../../Resources/Stylesheets/OnlineStyle.css" rel="stylesheet" /> <script src="../../../Resources/Code/prettify.js"> </script> <script src="../../../Resources/Code/lang-vb.js"> </script> <script src="../../../Resources/Code/jquery.min.js"> </script> <script src="../../../Resources/Code/ZeroClipboard.js"> </script> <script src="../../../SkinSupport/MadCapAll.js" type="text/javascript"> </script> </head> <body> <p class="MCWebHelpFramesetLink" style="display: none;"><a href="../../../../index_CSH.html#basics/querying/linq/linq_examples.htm" style="">Open topic with navigation</a> </p> <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../../../basics.htm">Basics Operations & Concepts</a><span class="MCBreadcrumbsDivider"> > </span><a class="MCBreadcrumbsLink" href="../../querying.htm">Querying</a><span class="MCBreadcrumbsDivider"> > </span><a class="MCBreadcrumbsLink" href="../linq.htm">LINQ</a><span class="MCBreadcrumbsDivider"> > </span><span class="MCBreadcrumbs">Simple LINQ Queries</span> </div> <p> <script type="text/javascript">/*<![CDATA[*/document.write('<a href="' + location.href +'">'); document.write("Direct Link"); document.write('</a>');/*]]>*/</script> </p> <p> </p> <h1>LINQ Examples</h1> <p>In order to learn like, take a look a the LINQ-resources and tutorials out on the Internet. For example the <a href="http://msdn.microsoft.com/en-us/library/bb397926.aspx" target="_blank">LINQ-Overview at MSDN</a> or at <a href="http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx" target="_blank">this example-collection.</a> Here are a few db4o LINQ examples.</p> <h2><a name="Simplest"></a>Simplest Possible Query</h2> <p>This query just returns all persons.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">var allPersons = from Person p in container select p;</pre> <div class="codesnippet-meta">LinqExamples.cs: Simple query <div class="codedownload"><a href="../../../CodeExamples/query/linq/Example-query-linq-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim allPersons = From p In container _ Select p</pre> <div class="codesnippet-meta">LinqExamples.vb: Simple query <div class="codedownload"><a href="../../../CodeExamples/query/linq/Example-query-linq-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <h2>Query For a Name</h2> <p>This query searches for a person by name.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">var allPersons = from Person p in container where p.Name.Equals("Joe") select p;</pre> <div class="codesnippet-meta">LinqExamples.cs: Query for name <div class="codedownload"><a href="../../../CodeExamples/query/linq/Example-query-linq-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim allPersons = From p As Person In container _ Where p.Name.Equals("Joe") _ Select p</pre> <div class="codesnippet-meta">LinqExamples.vb: Query for name <div class="codedownload"><a href="../../../CodeExamples/query/linq/Example-query-linq-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <h2>Query For a Certain Age</h2> <p>This queries for a certain age.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">var allPersons = from Person p in container where p.Age > 21 select p;</pre> <div class="codesnippet-meta">LinqExamples.cs: Query with a constraint <div class="codedownload"><a href="../../../CodeExamples/query/linq/Example-query-linq-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim allPersons = From p As Person In container _ Where p.Age > 21 _ Select p</pre> <div class="codesnippet-meta">LinqExamples.vb: Query with a constraint <div class="codedownload"><a href="../../../CodeExamples/query/linq/Example-query-linq-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <h2><a name="Sorting"></a><a name="kanchor9"></a>Sorting The Result Of A Query</h2> <p>Sort the results of a query.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">var allPersons = from Person p in container where p.Age > 21 orderby p.Name select p;</pre> <div class="codesnippet-meta">LinqExamples.cs: Use sorting on the query <div class="codedownload"><a href="../../../CodeExamples/query/linq/Example-query-linq-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim allPersons = From p As Person In container _ Where p.Age > 21 _ Order By p.Name _ Select p</pre> <div class="codesnippet-meta">LinqExamples.vb: Use sorting on the query <div class="codedownload"><a href="../../../CodeExamples/query/linq/Example-query-linq-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <h2><a name="Get"></a>Get The IQueryable Interface</h2> <p>You can get the .NET IQueryable interface for db4o</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IQueryable<Person> personQuerable = container.AsQueryable<Person>(); var adults = from p in personQuerable where p.Age > 18 orderby p.Name select p;</pre> <div class="codesnippet-meta">LinqExamples.cs: Get a IQueryable-instance <div class="codedownload"><a href="../../../CodeExamples/query/linq/Example-query-linq-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim personQuerable As IQueryable(Of Person) = container.AsQueryable(Of Person)() Dim adults = From p In personQuerable _ Where p.Age > 18 _ Order By p.Name _ Select p</pre> <div class="codesnippet-meta">LinqExamples.vb: Get a IQueryable-instance <div class="codedownload"><a href="../../../CodeExamples/query/linq/Example-query-linq-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <p> </p> <script type="text/javascript" src="../../../SkinSupport/MadCapBodyEnd.js"> </script> </body> </html>