Sophie

Sophie

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

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|SODA Query" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem">
    <head><title>SODA 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/soda/soda_example_catelog.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="../soda_query.htm">SODA Query</a><span class="MCBreadcrumbsDivider"> &gt; </span><span class="MCBreadcrumbs">Type Constraint</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>SODA Query Examples</h1>
        <p>Here's a collection of SODA-query examples.
		 These queries assume that there's a Pilot class with a name and age, 
			a Car class with a pilot and name and
			 a BlogPost class with list of tags, authors and a
			 <span class="PrimaryMap">Dictionary</span> of meta-data.</p>
        <p>There are also a few examples for special <a href="soda_special_case_examples.htm">cases.</a> <![CDATA[ ]]></p>
        <h2><a name="TypeConstraint"></a>Type Constraint</h2>
        <p>This is the most basic and most used constraint for SODA-queries. SODA acts like a filter on all stored objects. But usually you're only interested for instances of a certain type. Therefore you need to constrain the type of the result.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IQuery query = container.Query();
query.Constrain(typeof(Pilot));

IObjectSet result = query.Execute();</pre>
            <div class="codesnippet-meta">SodaQueryExamples.cs: Type constrain for the objects
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-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 query As IQuery = container.Query()
query.Constrain(GetType(Pilot))

Dim result As IObjectSet = query.Execute()</pre>
            <div class="codesnippet-meta">SodaQueryExamples.vb: Type constrain for the objects
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-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="FieldConstraint"></a>Field Constraint</h2>
        <p>You can add constrains on fields. This is done by descending into a field and constrain the value of that field. By default the constrain is an equality comparison.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IQuery query = container.Query();
query.Constrain(typeof (Pilot));
query.Descend("name").Constrain("John");

IObjectSet result = query.Execute();</pre>
            <div class="codesnippet-meta">SodaQueryExamples.cs: A simple constrain on a field
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-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 query As IQuery = container.Query()
query.Constrain(GetType(Pilot))
query.Descend("name").Constrain("John")

Dim result As IObjectSet = query.Execute()</pre>
            <div class="codesnippet-meta">SodaQueryExamples.vb: A simple constrain on a field
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-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="Comparisons"></a>Comparisons</h2>
        <p>You can do comparison on the field-values. For example to check if something is greater or smaller than something else. </p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IQuery query = container.Query();
query.Constrain(typeof (Pilot));
query.Descend("age").Constrain(42).Greater();

IObjectSet result = query.Execute();</pre>
            <div class="codesnippet-meta">SodaQueryExamples.cs: A greater than constrain
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-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 query As IQuery = container.Query()
query.Constrain(GetType(Pilot))
query.Descend("age").Constrain(42).Greater()

Dim result As IObjectSet = query.Execute()</pre>
            <div class="codesnippet-meta">SodaQueryExamples.vb: A greater than constrain
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-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.c#">
            <pre class="prettyprint" xml:space="preserve">IQuery query = container.Query();
query.Constrain(typeof (Pilot));
query.Descend("age").Constrain(42).Greater().Equal();

IObjectSet result = query.Execute();</pre>
            <div class="codesnippet-meta">SodaQueryExamples.cs: A greater than or equals constrain
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-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 query As IQuery = container.Query()
query.Constrain(GetType(Pilot))
query.Descend("age").Constrain(42).Greater().Equal()

Dim result As IObjectSet = query.Execute()</pre>
            <div class="codesnippet-meta">SodaQueryExamples.vb: A greater than or equals constrain
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-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="Combination"></a>Combination of Constraints (AND, OR)</h2>
        <p>You can combine different constraints with an 'AND' or 'OR' condition. By default all constrains are combined with the 'AND' condition. </p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IQuery query = container.Query();
query.Constrain(typeof (Pilot));
query.Descend("age").Constrain(42).Greater()
    .Or(query.Descend("age").Constrain(30).Smaller());

IObjectSet result = query.Execute();</pre>
            <div class="codesnippet-meta">SodaQueryExamples.cs: Logical combination of constrains
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-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 query As IQuery = container.Query()
query.Constrain(GetType(Pilot))
query.Descend("age").Constrain(42).Greater().[Or](query.Descend("age").Constrain(30).Smaller())

Dim result As IObjectSet = query.Execute()</pre>
            <div class="codesnippet-meta">SodaQueryExamples.vb: Logical combination of constrains
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-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="NotConstrain"></a>Not-Constrain</h2>
        <p>You can invert a constrain by calling the not-method.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IQuery query = container.Query();
query.Constrain(typeof (Pilot));
query.Descend("age").Constrain(42).Not();

IObjectSet result = query.Execute();</pre>
            <div class="codesnippet-meta">SodaQueryExamples.cs: Not constrain
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-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 query As IQuery = container.Query()
query.Constrain(GetType(Pilot))
query.Descend("age").Constrain(42).Not()

Dim result As IObjectSet = query.Execute()</pre>
            <div class="codesnippet-meta">SodaQueryExamples.vb: Not constrain
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-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="StringComparison"></a>String Comparison</h2>
        <p>There are special compare operations for strings. By default strings are compared by equality and the comparison is case sensitive.</p>
        <p>There's the contains-comparison which checks if a field contains a substring. The like-comparison is the case-insensitive version of the contains-comparison.</p>
        <p>Also a start-with- and a ends-with-caparison is available for strings. For this you can specify if the comparison is case sensitive or not.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IQuery query = container.Query();
query.Constrain(typeof (Pilot));
// First strings, you can use the contains operator
query.Descend("name").Constrain("oh").Contains()
    // Or like, which is like .contains(), but case insensitive
    .Or(query.Descend("name").Constrain("AnN").Like())
    // The .endsWith and .startWith constrains are also there,
    // the true for case-sensitive, false for case-insensitive
    .Or(query.Descend("name").Constrain("NY").EndsWith(false));

IObjectSet result = query.Execute();</pre>
            <div class="codesnippet-meta">SodaQueryExamples.cs: String comparison
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-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 query As IQuery = container.Query()
query.Constrain(GetType(Pilot))
' First strings, you can use the contains operator
' Or like, which is like .contains(), but case insensitive
' The .endsWith and .startWith constrains are also there,
' the true for case-sensitive, false for case-insensitive
query.Descend("name").Constrain("oh").Contains().[Or](query.Descend("name").Constrain("AnN").[Like]()).[Or](query.Descend("name").Constrain("NY").EndsWith(False))

Dim result As IObjectSet = query.Execute()</pre>
            <div class="codesnippet-meta">SodaQueryExamples.vb: String comparison
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-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="CheckForObject"></a>Compare Field With Existing Object</h2>
        <p>When you have a reference type field, you can compare this field with a certain object. It will compare the field and the object by object identity.</p>
        <p>Note that this comparison only works with stored objects. When you use a not yet stored object as constrain, it will use <a href="soda_special_case_examples.htm#MixQueryByExample">query by example</a>. To force a comparison by object identity, you can add a .Identiy() call.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">Pilot pilot = container.Query&lt;Pilot&gt;()[0];

IQuery query = container.Query();
query.Constrain(typeof(Car));
// if the given object is stored, its compared by identity
query.Descend("pilot").Constrain(pilot);

IObjectSet carsOfPilot = query.Execute();</pre>
            <div class="codesnippet-meta">SodaQueryExamples.cs: Compare with existing object
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-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 pilot As Pilot = container.Query(Of Pilot)()(0)

Dim query As IQuery = container.Query()
query.Constrain(GetType(Car))
' if the given object is stored, its compared by identity
query.Descend("pilot").Constrain(pilot)

Dim carsOfPilot As IObjectSet = query.Execute()</pre>
            <div class="codesnippet-meta">SodaQueryExamples.vb: Compare with existing object
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-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="DescendIntoObjects"></a>Descend Deeper Into Objects</h2>
        <p>You can descend deeper into the objects by following fields. This allows you to setup complex constraints on nested objects. Note that the deeper you descend into the objects, the more expensive the query is to execute.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IQuery query = container.Query();
query.Constrain(typeof (Car));
query.Descend("pilot").Descend("name").Constrain("John");

IObjectSet result = query.Execute();</pre>
            <div class="codesnippet-meta">SodaQueryExamples.cs: Descend over multiple fields
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-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 query As IQuery = container.Query()
query.Constrain(GetType(Car))
query.Descend("pilot").Descend("name").Constrain("John")

Dim result As IObjectSet = query.Execute()</pre>
            <div class="codesnippet-meta">SodaQueryExamples.vb: Descend over multiple fields
			<div class="codedownload"><a href="../../../CodeExamples/query/soda/Example-query-soda-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>