This is a follow up question to: Accessing dictionary in TransformResults failing
Given the following classes:
public class Product
{
public string Id { get; set; }
public string Name { get; set; }
public SpecialType DefaultOffer { get; set; }
public Dictionary<SpecialType, string> Specials { get; set; }
}
public enum SpecialType
{
None = 0,
Something1,
Something2
}
I create an index as follows:
public class ProductSummariesViaTransform : AbstractIndexCreationTask<Product>
{
public ProductSummariesViaTransform()
{
Map = products => from p in products
select new { p.Name };
TransformResults = (db, products) =>
from product in products
select new
{
Id = product.Id,
Name = product.Name,
SpecialOffer = product.Specials[product.DefaultOffer]
};
}
}
If I query using this index with .AsProjection<ProductSummary>() then SpecialOffer is always null, though Id and Name are properly populated.
However, if I change the transform to explicitly load the document (as shown below) then SpecialOffer gets populated as expected.
TransformResults = (db, products) =>
from product in products
let p = db.Load<Product>(product.Id) // explicit load
select new
{
Id = product.Id,
Name = product.Name,
SpecialOffer = p.Specials[p.DefaultOffer] // use explicit doc
};
Putting aside questions of whether or not a TransformResults is the best way to do this, it seems that it should not be necessary to load the Product document again in the Transform in order to be able to access the Specials dictionary.
My understanding is that the underlying document is always available to the Transform. Am I missing something or is this a bug?
The complete (passing) unit test is available at https://gist.github.com/4601829.
AsProjectionchanges the data that gets passed to the TransformResults.You need to us
AsorOfType, instead.