Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 2e9c43658e374d290a2de15d25134ac8 > files > 544

db4o-doc-8.0-1.fc15.i686.rpm

<?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 &amp; Concepts|Querying|Native Queries" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem">
    <head><title>Native Query 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/native_queries/native_query_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 &amp; Concepts</a><span class="MCBreadcrumbsDivider"> &gt; </span><a class="MCBreadcrumbsLink" href="../../querying.htm">Querying</a><span class="MCBreadcrumbsDivider"> &gt; </span><a class="MCBreadcrumbsLink" href="../native_queries.htm">Native Queries</a><span class="MCBreadcrumbsDivider"> &gt; </span><span class="MCBreadcrumbs">Equality</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>Native Query Examples</h1>
        <p>Here's a collection of native query examples. These queries assume that there's a Pilot class with a name and age and a Car class with a pilot and name.</p>
        <p MadCap:conditions="Primary..NET">Note that for .NET 3.5 or newer we recommend to use <a href="../linq.htm">LINQ</a> instead of native queries.</p>
        <h2><a name="Equality"></a>Equality</h2>
        <p>This query shows you how compare a <span class="Primarygetter/setter">property</span> for equality. In this example we compare the name of a person.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IList&lt;Pilot&gt; result = container.Query(
    delegate(Pilot pilot) { return pilot.Name == "John"; });</pre>
            <div class="codesnippet-meta">NativeQueryExamples.cs: Check for equality of the name
			<div class="codedownload"><a href="../../../CodeExamples/query/nq/Example-query-nq-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 result As IList(Of Pilot) = container.Query(Of Pilot)(AddressOf QueryJohns)</pre>
            <div class="codesnippet-meta">NativeQueryExamples.vb: Check for equality of the name
			<div class="codedownload"><a href="../../../CodeExamples/query/nq/Example-query-nq-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>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Private Shared Function QueryJohns(ByVal pilot As Pilot)
    Return pilot.Name = "John"
End Function</pre>
            <div class="codesnippet-meta">NativeQueryExamples.vb: Query for John
			<div class="codedownload"><a href="../../../CodeExamples/query/nq/Example-query-nq-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="Comparison"></a>Comparison</h2>
        <p>You can compare values with the usual comparison operators.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IList&lt;Pilot&gt; result = container.Query(
    delegate(Pilot pilot) { return pilot.Age &lt; 18; });</pre>
            <div class="codesnippet-meta">NativeQueryExamples.cs: Compare values to each other
			<div class="codedownload"><a href="../../../CodeExamples/query/nq/Example-query-nq-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 result As IList(Of Pilot) = container.Query(Of Pilot)(AddressOf QueryAdults)</pre>
            <div class="codesnippet-meta">NativeQueryExamples.vb: Compare values to each other
			<div class="codedownload"><a href="../../../CodeExamples/query/nq/Example-query-nq-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>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Private Shared Function QueryAdults(ByVal pilot As Pilot)
    Return pilot.Age &lt; 18
End Function</pre>
            <div class="codesnippet-meta">NativeQueryExamples.vb: Query for adults
			<div class="codedownload"><a href="../../../CodeExamples/query/nq/Example-query-nq-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="ValueRange"></a>Query For Value Range</h2>
        <p>Of course you can combine different comparisons. For example you can combine the greater and smaller than operators to check for a range of values.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IList&lt;Pilot&gt; result = container.Query(
    delegate(Pilot pilot) { return pilot.Age &gt; 18 &amp;&amp; pilot.Age &lt; 30; });</pre>
            <div class="codesnippet-meta">NativeQueryExamples.cs: Query for a particular rage of values
			<div class="codedownload"><a href="../../../CodeExamples/query/nq/Example-query-nq-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 result As IList(Of Pilot) = container.Query(Of Pilot)(AddressOf QueryRange)</pre>
            <div class="codesnippet-meta">NativeQueryExamples.vb: Query for a particular rage of values
			<div class="codedownload"><a href="../../../CodeExamples/query/nq/Example-query-nq-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>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Private Shared Function QueryRange(ByVal pilot As Pilot)
    Return pilot.Age &gt; 18 AndAlso pilot.Age &lt; 30
End Function</pre>
            <div class="codesnippet-meta">NativeQueryExamples.vb: Query for range
			<div class="codedownload"><a href="../../../CodeExamples/query/nq/Example-query-nq-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="LogicalOperators"></a>Combine Check With Logical Operators</h2>
        <p>Of course you can combine a arbitrary set of conditions with logical operators.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IList&lt;Pilot&gt; result = container.Query(
    delegate(Pilot pilot)
        {
            return (pilot.Age &gt; 18 &amp;&amp; pilot.Age &lt; 30)
                   || pilot.Name == "John";
        });</pre>
            <div class="codesnippet-meta">NativeQueryExamples.cs: Combine different comparisons with the logical operators
			<div class="codedownload"><a href="../../../CodeExamples/query/nq/Example-query-nq-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 result As IList(Of Pilot) = container.Query(Of Pilot)(AddressOf CombineCriterias)</pre>
            <div class="codesnippet-meta">NativeQueryExamples.vb: Combine different comparisons with the logical operators
			<div class="codedownload"><a href="../../../CodeExamples/query/nq/Example-query-nq-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>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Private Shared Function CombineCriterias(ByVal pilot As Pilot)
    Return (pilot.Age &gt; 18 AndAlso pilot.Age &lt; 30) OrElse pilot.Name = "John"
End Function</pre>
            <div class="codesnippet-meta">NativeQueryExamples.vb: Combine criterias
			<div class="codedownload"><a href="../../../CodeExamples/query/nq/Example-query-nq-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="SeperateQueryUnit"></a>Query In Separate<span MadCap:conditions="Primary..NET"> Method</span></h2>
        <p>You can implement your query in a separate<span MadCap:conditions="Primary..NET"> method</span> and then just us it where you need it. This is especially useful when you reuse the same query multiple times. Or you want to give your query a clear name for documentation purposes.</p>
        <p>First write your <span MadCap:conditions="Primary..NET"> method</span>:</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">private static bool AllJohns(Pilot pilot)
{
    return pilot.Name == "John";
}
</pre>
            <div class="codesnippet-meta">NativeQueryExamples.cs: Query as method
			<div class="codedownload"><a href="../../../CodeExamples/query/nq/Example-query-nq-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">Private Shared Function AllJohns(ByVal pilot As Pilot) As Boolean
    Return pilot.Name = "John"
End Function
</pre>
            <div class="codesnippet-meta">NativeQueryExamples.vb: Query as method
			<div class="codedownload"><a href="../../../CodeExamples/query/nq/Example-query-nq-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>And then use it:</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IList&lt;Pilot&gt; result = container.Query(new Predicate&lt;Pilot&gt;(AllJohns));</pre>
            <div class="codesnippet-meta">NativeQueryExamples.cs: Use the predefined query
			<div class="codedownload"><a href="../../../CodeExamples/query/nq/Example-query-nq-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 result As IList(Of Pilot) = container.Query(Of Pilot)(AddressOf AllJohns)</pre>
            <div class="codesnippet-meta">NativeQueryExamples.vb: Use the predefined query
			<div class="codedownload"><a href="../../../CodeExamples/query/nq/Example-query-nq-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="ArbitraryCode"></a>Arbitrary Code</h2>
        <p>In principal your query can contain any code and can do the most complex comparisons. However in practice the are limitations. The simple queries are <a href="native_query_optimization.htm">optimized and translated to SODA-queries</a>. This is not possible for complex queries. If the query cannot be optimized, db4o will instantiate all objects and pass it to your query-object. This is a order of magnitude slower than a optimized native query and only feasible for smaller data sets.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IList&lt;int&gt; allowedAges = Array.AsReadOnly(new int[] {18, 20, 35});
IList&lt;Pilot&gt; result = container.Query(
    delegate(Pilot pilot)
        {
            return allowedAges.Contains(pilot.Age) ||
                   pilot.Name.ToLowerInvariant() == "John";
        });</pre>
            <div class="codesnippet-meta">NativeQueryExamples.cs: Arbitrary code
			<div class="codedownload"><a href="../../../CodeExamples/query/nq/Example-query-nq-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 result As IList(Of Pilot) = container.Query(Of Pilot)(AddressOf QueryWithAnyCode)</pre>
            <div class="codesnippet-meta">NativeQueryExamples.vb: Arbitrary code
			<div class="codedownload"><a href="../../../CodeExamples/query/nq/Example-query-nq-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>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Private Shared Function QueryWithAnyCode(ByVal pilot As Pilot)
    Dim allowedAges As IList(Of Integer) = Array.AsReadOnly(New Integer() {18, 20, 35})
    Return allowedAges.Contains(Pilot.Age) _
                OrElse Pilot.Name.ToLowerInvariant() = "John"
End Function</pre>
            <div class="codesnippet-meta">NativeQueryExamples.vb: Query with arbitrary code
			<div class="codedownload"><a href="../../../CodeExamples/query/nq/Example-query-nq-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>&#160;</p>
        <script type="text/javascript" src="../../../SkinSupport/MadCapBodyEnd.js">
        </script>
    </body>
</html>