<?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|SODA Query" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem"> <head><title>SODA Special Cases 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_special_case_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="../soda_query.htm">SODA Query</a><span class="MCBreadcrumbsDivider"> > </span><span class="MCBreadcrumbs">Contains on Collections and Arrays</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 Special Cases Examples</h1> <p>This topic contains a examples which demonstrate special behavior for some types in SODA. Take also a look at the other <a href="soda_example_catelog.htm">SODA examples</a>.</p> <h2><a name="ContainsOnCollections"></a><a name="kanchor18"></a>Contains on Collections and Arrays</h2> <p>Collections and arrays have a special behavior in SODA to make them easier to query. For example you can simple use a constrain directly on a collection-field to check if it contains that value.</p> <p>Note that currently collections cannot be indexed and therefore such a constrain can be slow on a large data set.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IQuery query = container.Query(); query.Constrain(typeof (BlogPost)); query.Descend("tags").Constrain("db4o"); IObjectSet result = query.Execute();</pre> <div class="codesnippet-meta">SodaQueryExamples.cs: Collection contains 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(BlogPost)) query.Descend("tags").Constrain("db4o") Dim result As IObjectSet = query.Execute()</pre> <div class="codesnippet-meta">SodaQueryExamples.vb: Collection contains 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="ConstrainsOnCollectionMembers"></a>Constrains on Collection Members</h2> <p>When you have a collection or array field, you can simply descend further to the collection-member fields. This allows you query for a object, which has a collection and certain objects in that collection.</p> <p>Note that currently collections cannot be indexed and therefore such a constrain can be slow on a large data set.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IQuery query = container.Query(); query.Constrain(typeof(BlogPost)); query.Descend("authors").Descend("name").Constrain("Jenny"); IObjectSet result = query.Execute();</pre> <div class="codesnippet-meta">SodaQueryExamples.cs: Descend into collection members <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(BlogPost)) query.Descend("authors").Descend("name").Constrain("Jenny") Dim result As IObjectSet = query.Execute()</pre> <div class="codesnippet-meta">SodaQueryExamples.vb: Descend into collection members <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="ContainsKeyOnMaps"></a>Contains Key on <span class="PrimaryMap">Dictionary</span>s</h2> <p>You can check a dictionary if it contains a certain key. Similar to collections, you just can directly use a constrain on the collection field. This will compare the value with the keys of the <span class="PrimaryMap">Dictionary</span>.</p> <p>Note that currently collections cannot be indexed and therefore such a constrain can be slow on a large data set.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IQuery query = container.Query(); query.Constrain(typeof (BlogPost)); query.Descend("metaData").Constrain("source"); IObjectSet result = query.Execute();</pre> <div class="codesnippet-meta">SodaQueryExamples.cs: Dictionary contains a key 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(BlogPost)) query.Descend("metaData").Constrain("source") Dim result As IObjectSet = query.Execute()</pre> <div class="codesnippet-meta">SodaQueryExamples.vb: Dictionary contains a key 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="FieldObjects"></a>Return the Objects of a Field</h2> <p>With SODA you can navigate to a field and return the objects of that field. Note that this only works for reference objects and not for value objects like strings and numbers.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IQuery query = container.Query(); query.Constrain(typeof(Car)); query.Descend("name").Constrain("Mercedes"); // returns the pilot of these cars IObjectSet result = query.Descend("pilot").Execute();</pre> <div class="codesnippet-meta">SodaQueryExamples.cs: Return the object of 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(Car)) query.Descend("name").Constrain("Mercedes") ' returns the pilot of these cars Dim result As IObjectSet = query.Descend("pilot").Execute()</pre> <div class="codesnippet-meta">SodaQueryExamples.vb: Return the object of 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="MixQueryByExample"></a>Mixing With Query By Example</h2> <p>When you have a reference type field, you can also use a <a href="../query_by_example.htm">query by example</a> constrain for that field. Pass a new object as an example for this.</p> <p>Note that when you pass a persisted object, it will compare it by object identity and not use it as example. You can force this behavior by adding an explicit by example constrain.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IQuery query = container.Query(); query.Constrain(typeof(Car)); // if the given object is not stored, // it will behave like query by example for the given object Pilot examplePilot = new Pilot(null, 42); query.Descend("pilot").Constrain(examplePilot); IObjectSet carsOfPilot = query.Execute();</pre> <div class="codesnippet-meta">SodaQueryExamples.cs: Mix with query by example <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)) ' if the given object is not stored, ' it will behave like query by example for the given object Dim examplePilot As New Pilot(Nothing, 42) query.Descend("pilot").Constrain(examplePilot) Dim carsOfPilot As IObjectSet = query.Execute()</pre> <div class="codesnippet-meta">SodaQueryExamples.vb: Mix with query by example <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="DynamicTypedNature"></a>Dynamically Typed</h2> <p>SODA is a dynamically query language. By default SODA acts like a filter on all stored objects. You just add constrains which filters the objects to the desired output. </p> <p>An example for this behavior: You just add an field-constraint without any <a href="soda_example_catelog.htm#TypeConstraint">type-constrain</a> on the object. This will return all objects which have such a field and match the constrain.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IQuery query = container.Query(); // You can simple filter objects which have a certain field query.Descend("name").Constrain(null).Not(); IObjectSet result = query.Execute();</pre> <div class="codesnippet-meta">SodaQueryExamples.cs: Pure field 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() ' You can simple filter objects which have a certain field query.Descend("name").Constrain(Nothing).[Not]() Dim result As IObjectSet = query.Execute()</pre> <div class="codesnippet-meta">SodaQueryExamples.vb: Pure field 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> <p>This also means that you can query for not existing fields. SODA will not complain if a field doesn't exist. Instead it won't return any object, because no object could satisfy the constrain.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IQuery query = container.Query(); query.Constrain(typeof (Pilot)); // using not existing fields doesn't throw an exception // but rather exclude all object which don't use this field query.Descend("notExisting").Constrain(null).Not(); IObjectSet result = query.Execute();</pre> <div class="codesnippet-meta">SodaQueryExamples.cs: Using not existing fields excludes 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)) ' using not existing fields doesn't throw an exception ' but rather exclude all object which don't use this field query.Descend("notExisting").Constrain(Nothing).[Not]() Dim result As IObjectSet = query.Execute()</pre> <div class="codesnippet-meta">SodaQueryExamples.vb: Using not existing fields excludes 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> <script type="text/javascript" src="../../../SkinSupport/MadCapBodyEnd.js"> </script> </body> </html>