locked
Why does my query for a single object look like this? RRS feed

  • Question

  • I've got a search screen connected to a WCF RIA Service data source (which runs a custom LINQ provider).

    I run the app and the data loads (45 'rows').  I click on the summary value of one row to load the details.  I've turned tracing on and this is what I see:

     

    source.Where(u => (value(LightSwitchApplication.Implementation.LinqToLdapServiceDataServiceImplementation+<>c__DisplayClass2).objectGuid.HasValue AndAlso (Convert(u.objectGuid) == value(LightSwitchApplication.Implementation.LinqToLdapServiceDataServiceImplementation+<>c__DisplayClass2).objectGuid)))
    

     

    objectGuid is the [Key] on the UserObject class which the query is returning.

    The trace also shows the query arguments:

    Query Arguments: QueryOperationParameter:: Name = 'objectGuid', ID = 'LightSwitchApplication:LinqToLdapServiceData/Members[UserObjects_SingleOrDefault]/Parameters[objectGuid]': 50aa3a5f-d916-485e-893f-7190c6c0d3d1

    Can someone put into words what this query is trying to achieve, please?  It looks a lot more complicated than:

    source.Where(objectGuid == "50aa3a5f-d916-485e-893f-7190c6c0d3d1")
    

    which is what I was expecting it to look like.  It seems to be confusing the LINQ provider - I'm trying to help the author sort out why it's not working but it doesn't help that I don't know LINQ.

    Thursday, December 22, 2011 7:42 PM

Answers

All replies

  • The underlying type of the query parameter is a nullable type therefore in order to avoid causing the custom RIA data service from throwing a NullReferenceException when the parameter is null, LightSwitch injects the appropriate null checks so it is easier to write the custom Ria service.

    Assumption:  objectGuid is a Nullable<Guid>

    Therefore the query is essentially the following:  source.Where(u=> objectGuid.HasValue && u.objectGuid == objectGuid.Value)

    Thursday, December 22, 2011 8:43 PM
  • Thanks for the reply.  objectGuid is defined as the [Key] like this:
        public class UserObject
        {
            [Key]
            public Guid objectGuid { get; set; }
            : 
            :
        
    

    in the RIA Service, with the default query like this:
            [Query(IsDefault = true)]
            public IQueryable<UserObject> GetUserObjects()
            {
                if (context == null)
                    context = new DirectoryContext();
    
                var users = context.Query<UserObject>();
            }
    

    Any idea why it thinks objectGuid is Nullable?  It's not marked as nullable and I would've thought as a [Key] it couldn't be.

    • Edited by SSG31415926 Friday, December 23, 2011 7:38 AM
    Friday, December 23, 2011 7:37 AM
  • It is not that the objectGuid is nullable, it is that the type of the query parameter is nullable.  Is this a modeled query that is being executed?
    Tuesday, January 3, 2012 3:26 PM
  • I don't know what you mean by modeled query.

    Wednesday, January 11, 2012 6:27 AM
  • Modelled query = a query that you created in LightSwitch, based on an entity.
    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    If you find a reply helpful, please click "Vote as Helpful", if a reply answers your question, please click "Mark as Answer"
     
    By doing this you'll help people find answers faster.
    Wednesday, January 11, 2012 11:32 AM
    Moderator
  • Yes, it was a modelled query.
    Wednesday, January 11, 2012 4:58 PM