Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9173001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:29:12+00:00 2026-06-17T16:29:12+00:00

Given this document class: public class Product { public string Id { get; set;

  • 0

Given this document class:

    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
    {
        Something1,
        Something2
    }

And this view model that I wish to project from the above document:

    public class ProductSummary
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string SpecialOffer { get; set; }
    }

I have created the following index:

    public class ProductSummaries : AbstractIndexCreationTask<Product>
    {
        public ProductSummaries()
        {
            Map = products => from p in products
                              select new { p.Id, p.Name, p.DefaultOffer, p.Specials };

            TransformResults = (db, products) =>
                                from p in products
                                select new
                                {
                                    Id = p.Id,
                                    Name = p.Name,
                                    SpecialOffer = p.Specials[p.DefaultOffer.Value]
                                };
        }
    }

In simple terms, I want the view model to use whichever of the strings in the Specials dictionary is indicated by the current value of DefaultOffer.

The following unit test fails:

    [TestMethod]
    public void CanIndexIntoDictionary()
    {
        using (var documentStore = this.GetDocumentStore())
        {
            documentStore.ExecuteIndex(new ProductSummaries());

            // Store some documents
            using (var session = documentStore.OpenSession())
            {
                session.Store(new Product 
                { 
                    Id = "products/2", 
                    Name = "B", 
                    Specials = new Dictionary<SpecialType, string> 
                    { 
                        { SpecialType.Something1, "B1" }, 
                        { SpecialType.Something2, "B2" } 
                    }, 
                    DefaultOffer = SpecialType.Something2 
                 });
                 session.SaveChanges();
            }

            // Make sure it got persisted correctly
            using (var session = documentStore.OpenSession())
            {
                var b = session.Load<Product>("products/2");
                Assert.AreEqual("B2", b.Specials[b.DefaultOffer.Value]); // PASSES
            }

            // Now query and transform
            using (var session = documentStore.OpenSession())
            {
                var result = session.Query<Product, ProductSummaries>()
                    .Customize(x => x.WaitForNonStaleResults())
                    .AsProjection<ProductSummary>()
                    .ToList();

                Assert.AreEqual(1, result.Count);
                Assert.AreEqual("B2", result.First().SpecialOffer); // FAILS - actual is NULL
            }
        }
    }

What do I need to do to make this test pass?

* UPDATE *

By using Matt’s suggestion (in the comments below) of having an Enum value that represents NONE we can modify his answer and get rid of the nullable enum. The whole model and index looks a lot cleaner.

    public enum SpecialType
    {
        None = 0,
        Something1,
        Something2
    }

    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 class ProductSummaries : AbstractIndexCreationTask<Product,ProductSummary>
    {
        public ProductSummaries()
        {
            Map = products => from p in products
                              select new { p.Name, SpecialOffer = p.Specials[p.DefaultOffer] };

            Store(x => x.SpecialOffer, FieldStorage.Yes);
        }
    }

Interestingly, this index eliminates the need for null checking and the like because RavenDB simply sets SpecialOffer to null when p.DefaultOffer is not a key contained in the Specials dictionary. (This is only true when p.Name is included in the Map.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:29:13+00:00Added an answer on June 17, 2026 at 4:29 pm

    You don’t need a TransformResults section in the index. In fact, you’re mapping quite a bit that you don’t need.

    • Id is always mapped implicitly. It becomes __document_id in the index and raven hooks it up appropriately.
    • Name only needs to be mapped if you are going to filter or sort by it, which you’re not doing in the test. You can put it back if you need it for the real world.
    • Similar to Name, you only need to map DefaultOffer and Specials if you are going to use them for other purposes. For the sake of demonstration and passing your unit test, I’ve removed them.

    The only trickery required here is due to the nullable enum. Because of that, Raven has a hard time translating your query from c#. You can work around it with some creative null checks and use of the AsDocument method:

    public class ProductSummaries : AbstractIndexCreationTask<Product, ProductSummary>
    {
        public ProductSummaries()
        {
            Map = products => from p in products
                              let defaultOffer = AsDocument(p).Value<string>("DefaultOffer")
                              select new
                              {
                                  SpecialOffer = defaultOffer == null ? null : AsDocument(p.Specials)[defaultOffer]
                              };
    
            Store(x => x.SpecialOffer, FieldStorage.Yes);
        }
    }
    

    Also note that the reason you were getting null on the special offer field before was because you were trying to project it from the index but it wasn’t a stored field. Turning field storage on for that field will solve that part of the problem.

    You don’t need to store any of the other fields, because they already exist on the document – which is also a source of data for projection.

    BTW – thanks for the unit test. It makes it much easier to debug and answer quickly. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a document that looks like this: public class Post { public string Title
Take this example model: public class Location { public string Id { get; set;
Given the following classes: public class Lookup { public string Code { get; set;
Given this structure: <root> <user> <userName>user1</userName> <userImageLocation>/user1.png</userImageLocation> </user> <user> <userName>user2</userName> </user> </root> public class
Say I have the given document structure in RavenDb public class Car { public
Given a document like this: <html xmlns=http://www.w3.org/1999/xhtml xml:lang=de> <body> ... </body> How can I
Given an XML document like this: <!DOCTYPE doc SYSTEM 'http://www.blabla.com/mydoc.dtd'> <author>john</author> <doc> <title>&title;</title> </doc>
Given this : delimiter // create procedure setup() begin declare d datetime; set d
I read this about class in the C++ standard document: A class is a
I am writing a c++ app to implement this: Given an arbitrary text document

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.