Note that there are some explanatory texts on larger screens.

plurals
  1. POWCF Data Services query projection with nullable navigation properties
    primarykey
    data
    text
    <p>I'm trying to write a query against a WCF data service that performs a projection that includes nullable navigation properties (i.e. the FK column in the underlying data model is nullable, so the association is 0..1). The project language is VB.NET, not by my choice. Example portion of query:</p> <pre><code>From t In DataServiceReference.Context.Transactions Where t.ID = transactionID Select CSM = t.WrittenByEmployee.Name, _ t.CustomerFirstName </code></pre> <p>When this query is executed against a row where WrittenByEmployee is null, I get the following error, as one would expect:</p> <blockquote> <p>An entry returned by the navigation property 'WrittenByEmployee' is null and cannot be initialized. You should check for a null value before accessing this property.</p> </blockquote> <p>According to <a href="http://blogs.msdn.com/b/marcelolr/archive/2010/05/10/wcf-data-service-projections-null-handling-and-expansion.aspx?Redirected=true" rel="nofollow">this blog</a> conditional null checking is possible in C# by using the ternary ?: operator. However, sadly the project is in VB.NET and not C#. I know that the equivalent in VB is the If() operator, but it does not appear to work as desired. If I alter the above query to this:</p> <pre><code>Select CSM = If(t.WrittenByEmployee IsNot Nothing, t.WrittenByEmployee.Name, String.Empty) </code></pre> <p>I get this error at runtime:</p> <blockquote> <p>Constructing or initializing instances of the type (...) with the expression (t.WrittenBy Employee != null) is not supported</p> </blockquote> <p>I tried inverting the test but got a similar error, only with (t.WrittenByEmployee == null) instead.</p> <p>How can I compose this query to check the WrittenByEmployee navigation property for nulls in a way that Data Services will accept?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload