locked
Azure Table Storage: What is the workaround for not having ElementAt functionality? RRS feed

  • Question

  • Hello,

    I'm more a SQL guy getting into LINQ-style CRUD ops for Azure Table Storage. Using a For-Next loop is easy to modify properties across the board:

    For Each entity As MyTableServiceEntityClass In partitionQuery
        entity.Url = "ReplacementValue"
        serviceContext.UpdateObject(entity)
    Next
    serviceContext.SaveChangesWithRetries()

    but what if I need to access individual entities, modify their properties, then update ... along the lines of something like this:

    Dim entity as MyTableServiceEntityClass
    For i = 25 to 28
        entity = partitionQuery.ElementAt(i)
        entity.Url = "ReplacementValue"
        serviceContext.UpdateObject(entity)
    Next
    serviceContext.SaveChangesWithRetries()

    ElementAt isn't supported. I have some sorting and property swaps to make on individual entities that I'm doing an update on ... I'd like to get a specific reference to individual elements in the CloudTableQuery result set. Please provide a quick code sample if you can ... I'm very good at SQL but very new to LINQ-style syntax. Thanks!

    Tuesday, October 2, 2012 7:21 PM

Answers

  • Hi,

    You can use the ToArray<T> (or ToList<T>) methods on the query to convert the query to an array or list, which will be easyer to work with and allow you to access the elements.

    Regards,

    Alan


    Free EBook: "Windows Azure Service Bus Developer Guide" http://www.cloudcasts.net/devguide/

    • Marked as answer by SellRex Tuesday, October 2, 2012 9:18 PM
    Tuesday, October 2, 2012 7:30 PM

All replies

  • Hi,

    You can use the ToArray<T> (or ToList<T>) methods on the query to convert the query to an array or list, which will be easyer to work with and allow you to access the elements.

    Regards,

    Alan


    Free EBook: "Windows Azure Service Bus Developer Guide" http://www.cloudcasts.net/devguide/

    • Marked as answer by SellRex Tuesday, October 2, 2012 9:18 PM
    Tuesday, October 2, 2012 7:30 PM
  • Does the update-part of the code work the same way after you use .ToArray or .ToList on it? ... or do you have to convert back to something in order to perform the update [i.e., serviceContext.UpdateObject(entity)
    ]?
    Tuesday, October 2, 2012 7:44 PM
  • Hi,

    Yes I think it will (don't have VS open so I cant test though...).

    Regards,

    Alan


    Free EBook: "Windows Azure Service Bus Developer Guide" http://www.cloudcasts.net/devguide/

    Tuesday, October 2, 2012 8:19 PM
  • Code sample might be needed ... I'm not familiar with this type of coding. I'm a SQL guy. This doesn't work:

    Dim entityList As List(Of MyTableServiceEntityClass) = partitionQuery.ToList

    Exception: Can't be converted to generic list

    Tuesday, October 2, 2012 8:27 PM
  • Ok, it seems to accept this:

    Dim entityList As List(Of MyTableServiceEntityClass) = partitionQuery.Execute.ToList

    I'll find out in a sec if this works.


    • Edited by SellRex Tuesday, October 2, 2012 8:43 PM
    Tuesday, October 2, 2012 8:43 PM
  • Thanks for the tip Alan. Yes ... this works:

    Dim entityList As List(Of MyTableServiceEntityClass) = partitionQuery.Execute.ToList
    Dim entity as MyTableServiceEntityClass
    For i = 25 to 28
        entity = entityList.ElementAt(i)
        entity.Url = "ReplacementValue"
        serviceContext.UpdateObject(entity)
    Next
    serviceContext.SaveChangesWithRetries()

    This should have been added as an example at: http://www.windowsazure.com/en-us/develop/net/how-to-guides/table-services/


    • Edited by SellRex Tuesday, October 2, 2012 9:19 PM
    Tuesday, October 2, 2012 9:17 PM