<?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" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem"> <head><title>LINQ </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.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><span class="MCBreadcrumbs">LINQ </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><a name="kanchor7"></a>LINQ</h1> <p>Of course, db4o supports .NET LINQ to query the database.</p> <p>First, you need to add the <i>Db4objects.Db4o.Linq.dll</i>-assembly to your project. This assembly contains db4o LINQ-Implementation. Make also sure that Mono.Reflection.dll is aside it. <a href="../../dependency_overview.htm" target="" title="" alt="" class="MCXref" xrefformat="See "{paratext}"">See "Dependency Overview"</a></p> <p>Note that LINQ requires .NET 3.5 or newer!</p> <h2>LINQ Queries</h2> <p>Writing a query is simple. You write the LINQ query against the object-container. First you need to import the LINQ-namespaces.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">using System.Linq; using Db4objects.Db4o.Linq;</pre> <div class="codesnippet-meta">LinqExamples.cs: Use the LINQ namespace <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">Imports System.Linq Imports Db4objects.Db4o.Linq</pre> <div class="codesnippet-meta">LinqExamples.vb: Use the LINQ namespace <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>After that, you can start to write a LINQ-Query against the object container.</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> <p>In order to learn LINQ, take a look a the LINQ-resources and tutorial 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></p> <p>There are also a few simple <a href="linq/linq_examples.htm">LINQ query examples for db4o here</a>.</p> <h2><a name="kanchor8"></a>IQueryable</h2> <p>db4o also support the .NET IQueryable interface. This is useful when you want to build an abstraction layer for db4o or integrate db4o with other frameworks.</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> <h2>More on db4o's LINQ Provider</h2> <p>db4o LINQ provider translates the LINQ queries into db4o's low level query <a href="soda_query.htm">API</a>. Read what this query optimization need and what its limits are. <a href="linq/optimization.htm" target="" title="" alt="" class="MCXref" xrefformat="See "{paratext}"">See "LINQ Optimization"</a></p> <p>Also read how LINQ is supported on the .NET Compact Framework. <a href="linq/linq_for_compact_framework.htm" target="" title="" alt="" class="MCXref" xrefformat="See "{paratext}"">See "LINQ For Compact Framework"</a></p> <p> </p> <script type="text/javascript" src="../../SkinSupport/MadCapBodyEnd.js"> </script> </body> </html>