Search code examples
c#amazon-dynamodbobject-persistence

Why doesn't IEnumerable.Where() find my objects in DynamoDBContext.Scan() results?


While using AWS DynamoDB "Object Persistence Model" in C#, ran into an interesting issue; parsing results of a Scan operation. In the code below, my entries in Datastore.userIndicators (which is a Dictionary with Lists of objects, indexed by usernames) are always empty lists.

var allIndicators = context.Scan<Indicator>();

Datastore.globalIndicators = allIndicators.Where(i => i.UserName == "*").ToList();
var userDefinedIndicators = allIndicators.Where(i => i.UserName != "*");

foreach (var username in userDefinedIndicators.Select(i => i.UserName).Distinct())
{
    Datastore.userIndicators[username] = userDefinedIndicators.Where(i => i.DynamoDbRangeKey.StartsWith(username)).ToList();
}

So, for example, if I have entries in my table that include an attribute "UserName" with value "userA", when running this code the Dictionary "Datastore.userIndicators" will end up with an entry for key "userA" but the corresponding value will be an empty list.


Solution

  • After fiddling with this and following a hunch, I modified the assignment of

    var allIndicators = context.Scan<Indicator>();
    

    to

    var allIndicators = context.Scan<Indicator>().ToList();
    

    Voila!

    It turns out (as confirmed by AWS SDK documentation), the return of the DynamoDBContext.Scan() method is lazy-loaded. Calling .ToList() forces enumeration and loads all the results.