Sophie

Sophie

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

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|Query By Example" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem">
    <head><title>Query By Example 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/query_by_example/qby_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="../query_by_example.htm">Query By Example</a><span class="MCBreadcrumbsDivider"> &gt; </span><span class="MCBreadcrumbs">Query By Example Basics</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>Query By Example Examples</h1>
        <h2><a name="Basics"></a>Query By Example Basics</h2>
        <p>For a query you pass in an example object to db4o. db4o will exanimate the object with reflection. Each field which doesn't have the default value will be use as a constrain. This means a number-field which isn't zero, a reference which isn't null or a string which isn't null. In this example we set the name to John. Then db4o will return all pilots with the name John.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">Pilot theExample = new Pilot();
theExample.Name = "John";
IList result = container.QueryByExample(theExample);</pre>
            <div class="codesnippet-meta">QueryByExamples.cs: Query for John by example
			<div class="codedownload"><a href="../../../CodeExamples/query/qbe/Example-query-qbe-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 theExample As New Pilot()
theExample.Name = "John"
Dim result As IList = container.QueryByExample(theExample)</pre>
            <div class="codesnippet-meta">QueryByExamples.vb: Query for John by example
			<div class="codedownload"><a href="../../../CodeExamples/query/qbe/Example-query-qbe-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>Or we set the age to 33 and db4o will return all 33 years old pilots.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">Pilot theExample = new Pilot();
theExample.Age = 33;
IList result = container.QueryByExample(theExample);</pre>
            <div class="codesnippet-meta">QueryByExamples.cs: Query for 33 year old pilots
			<div class="codedownload"><a href="../../../CodeExamples/query/qbe/Example-query-qbe-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 theExample As New Pilot()
theExample.Age = 33
Dim result As IList = container.QueryByExample(theExample)</pre>
            <div class="codesnippet-meta">QueryByExamples.vb: Query for 33 year old pilots
			<div class="codedownload"><a href="../../../CodeExamples/query/qbe/Example-query-qbe-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="Combine"></a>Combine Values</h2>
        <p>When you set multiple values all will be used as constrain. For example when we set the name to Jo and the age to 29 db4o will return all pilots which are 29 years with the name Jo.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">Pilot theExample = new Pilot();
theExample.Name = "Jo";
theExample.Age = 29;
IList result = container.QueryByExample(theExample);</pre>
            <div class="codesnippet-meta">QueryByExamples.cs: Query a 29 years old Jo
			<div class="codedownload"><a href="../../../CodeExamples/query/qbe/Example-query-qbe-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 theExample As New Pilot()
theExample.Name = "Jo"
theExample.Age = 29
Dim result As IList = container.QueryByExample(theExample)</pre>
            <div class="codesnippet-meta">QueryByExamples.vb: Query a 29 years old Jo
			<div class="codedownload"><a href="../../../CodeExamples/query/qbe/Example-query-qbe-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="AllOfAType"></a>All Objects Of A Type</h2>
        <p>If you pass a empty example db4o will return all objects of that type. </p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">Pilot example = new Pilot();
IList result = container.QueryByExample(example);</pre>
            <div class="codesnippet-meta">QueryByExamples.cs: All objects of a type by passing a empty example
			<div class="codedownload"><a href="../../../CodeExamples/query/qbe/Example-query-qbe-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 example As New Pilot()
Dim result As IList = container.QueryByExample(example)</pre>
            <div class="codesnippet-meta">QueryByExamples.vb: All objects of a type by passing a empty example
			<div class="codedownload"><a href="../../../CodeExamples/query/qbe/Example-query-qbe-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>Alternatively you also can directly pass in the type.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IList result = container.QueryByExample(typeof (Pilot));</pre>
            <div class="codesnippet-meta">QueryByExamples.cs: All objects of a type by passing the type
			<div class="codedownload"><a href="../../../CodeExamples/query/qbe/Example-query-qbe-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 = container.QueryByExample(GetType(Pilot))</pre>
            <div class="codesnippet-meta">QueryByExamples.vb: All objects of a type by passing the type
			<div class="codedownload"><a href="../../../CodeExamples/query/qbe/Example-query-qbe-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="AllObjects"></a>All Objects</h2>
        <p>When you pass null all objects stored in the database will be returned.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IList result = container.QueryByExample(null);</pre>
            <div class="codesnippet-meta">QueryByExamples.cs: All objects
			<div class="codedownload"><a href="../../../CodeExamples/query/qbe/Example-query-qbe-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 = container.QueryByExample(Nothing)</pre>
            <div class="codesnippet-meta">QueryByExamples.vb: All objects
			<div class="codedownload"><a href="../../../CodeExamples/query/qbe/Example-query-qbe-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="NestedObjects"></a>Nested Objects</h2>
        <p>You can also use nested objects as an example. For example with a car and a pilot. We can query for a car which has a pilot with certain constrains. In this example we get the cars which pilot is called Jenny.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">Pilot pilotExample = new Pilot();
pilotExample.Name = "Jenny";

Car carExample = new Car();
carExample.Pilot = pilotExample;
IList result = container.QueryByExample(carExample);</pre>
            <div class="codesnippet-meta">QueryByExamples.cs: Nested objects example
			<div class="codedownload"><a href="../../../CodeExamples/query/qbe/Example-query-qbe-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 pilotExample As New Pilot()
pilotExample.Name = "Jenny"

Dim carExample As New Car()
carExample.Pilot = pilotExample
Dim result As IList = container.QueryByExample(carExample)</pre>
            <div class="codesnippet-meta">QueryByExamples.vb: Nested objects example
			<div class="codedownload"><a href="../../../CodeExamples/query/qbe/Example-query-qbe-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="Contains"></a>Contains Example</h2>
        <p>Collections and arrays act a little different. Query by example returns all object which have at least the items in the array or collection from the example. For example it returns the blog post which has the "db4o"-tag in its tag-collection. </p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">BlogPost pilotExample = new BlogPost();
pilotExample.AddTags("db4o");
IList result = container.QueryByExample(pilotExample);</pre>
            <div class="codesnippet-meta">QueryByExamples.cs: Contains in collections
			<div class="codedownload"><a href="../../../CodeExamples/query/qbe/Example-query-qbe-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 pilotExample As New BlogPost()
pilotExample.AddTags("db4o")
Dim result As IList = container.QueryByExample(pilotExample)</pre>
            <div class="codesnippet-meta">QueryByExamples.vb: Contains in collections
			<div class="codedownload"><a href="../../../CodeExamples/query/qbe/Example-query-qbe-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="StructuredCombine"></a>Structured Contains</h2>
        <p>You can even check that a item in a collection fulfills certain criteria's. For example we can check that the blog post has an author with the name John in its author-collection. </p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">BlogPost pilotExample = new BlogPost();
pilotExample.AddAuthors(new Author("John"));
IList result = container.QueryByExample(pilotExample);</pre>
            <div class="codesnippet-meta">QueryByExamples.cs: Structured contains
			<div class="codedownload"><a href="../../../CodeExamples/query/qbe/Example-query-qbe-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 pilotExample As New BlogPost()
pilotExample.AddAuthors(New Author("John"))
Dim result As IList = container.QueryByExample(pilotExample)</pre>
            <div class="codesnippet-meta">QueryByExamples.vb: Structured contains
			<div class="codedownload"><a href="../../../CodeExamples/query/qbe/Example-query-qbe-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>
        <script type="text/javascript" src="../../../SkinSupport/MadCapBodyEnd.js">
        </script>
    </body>
</html>