none
Syntax - How would I get a single value? RRS feed

  • Question

  • I need to get the GUID from a row.

     

    var query = (from entity in context.CreateQuery<Image>("Images")
                    where entity.IdentityGUID.Equals(_StrIdentityGUID)
                    //select entity);
                    select entity.BlobGUID);
    
    Or....
    
    IQueryable<string> x  = (from entity in context.CreateQuery<Image>("Images")
                        where entity.IdentityGUID.Equals(_StrIdentityGUID)
                        //select entity);
                        select new { _BlobGUID = entity.BlobGUID }).AsEnumerable().Select(t => t._BlobGUID);
    
    
    

     

    Then for the UI...
    
    imgCrop.ImageUrl = query.Take(1);
    
    Or...
    
    imgCrop.ImageUrl = query.ToString();
    

     

    I'm still trying to get my head around Linq...

    Thanks for anything, Robert

     

    Tuesday, October 18, 2011 1:47 AM

Answers

  • Hi Rob,

    You are using WCF Data Service, right? I think FirstOrDefault() method should work.

    var query = (from entity in context.CreateQuery<Image>("Images")
                    where entity.IdentityGUID.Equals(_StrIdentityGUID)
                    //select entity);
                    select entity.BlobGUID);
    var first= query.FristOrDefault();
    

    You can use SQL Profiler to watch the "T-SQL" generated by the LINQ query. You should set the rules to let entity sets visible:

    config.SetEntitySetAccessRule("Images", EntitySetRights.AllRead);
    
    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, December 7, 2011 6:32 AM
    Moderator

All replies

  • Hi,

    If I understand you at all, what about FirstOrDefault?

    imgCrop.ImageUrl = query.FirstOrDefault() ?? "";
    
    

    Best regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    • Proposed as answer by Alan_chenModerator Wednesday, October 19, 2011 2:58 AM
    • Marked as answer by Alan_chenModerator Monday, October 31, 2011 2:08 AM
    • Unmarked as answer by Rob Vig Wednesday, December 7, 2011 1:50 AM
    • Unproposed as answer by Rob Vig Wednesday, December 7, 2011 1:50 AM
    Tuesday, October 18, 2011 1:49 PM
  • Hi,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 24, 2011 1:53 AM
    Moderator
  • Nope. That didn't work.

     

    imgCrop.ImageUrl = query.Take(1);

    Or...
    
    imgCrop.ImageUrl = query.ToString();
    
    Or...
    
    imgCrop.ImageUrl = query.FirstOrDefault();
    

    all of which don't work...

    Wednesday, December 7, 2011 2:34 AM
  • Hi Rob,

    You are using WCF Data Service, right? I think FirstOrDefault() method should work.

    var query = (from entity in context.CreateQuery<Image>("Images")
                    where entity.IdentityGUID.Equals(_StrIdentityGUID)
                    //select entity);
                    select entity.BlobGUID);
    var first= query.FristOrDefault();
    

    You can use SQL Profiler to watch the "T-SQL" generated by the LINQ query. You should set the rules to let entity sets visible:

    config.SetEntitySetAccessRule("Images", EntitySetRights.AllRead);
    
    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, December 7, 2011 6:32 AM
    Moderator
  • What is your data source?

    And what do you mean by "don't work"? Do you get an exception?

    If you're using a transaction-based data store, you may need to execute your query in the data layer and not in the UI. In other words, don't return an IQueryable object and execute it in the UI. Call .FirstOrDefault() on the query and return the result instead.


    Check out My Blog for tech news, development tips, and other information for geeks like me.
    Thursday, December 8, 2011 6:33 PM