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 8781047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:12:03+00:00 2026-06-13T20:12:03+00:00

Given the following classes: public class Lookup { public string Code { get; set;

  • 0

Given the following classes:

public class Lookup
{
    public string Code { get; set; }
    public string Name { get; set; }
}

public class DocA
{
    public string Id { get; set; }
    public string Name { get; set; }
    public Lookup Currency { get; set; }
}

public class ViewA // Simply a flattened version of the doc
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string CurrencyName { get; set; } // View just gets the name of the currency
}

I can create an index that allows client to query the view as follows:

public class A_View : AbstractIndexCreationTask<DocA, ViewA>
{
    public A_View()
    {
        Map = docs => from doc in docs
                      select new ViewA
                      {
                          Id = doc.Id,
                          Name = doc.Name,
                          CurrencyName = doc.Currency.Name
                      };

        Reduce = results => from result in results
                      group on new ViewA
                      {
                          Id = result.Id,
                          Name = result.Name,
                          CurrencyName = result.CurrencyName
                      } into g
                      select new ViewA
                      {
                          Id = g.Key.Id,
                          Name = g.Key.Name,
                          CurrencyName = g.Key.CurrencyName
                      };
    }
}

This certainly works and produces the desired result of a view with the data transformed to the structure required at the client application. However, it is unworkably verbose, will be a maintenance nightmare and is probably fairly inefficient with all the redundant object construction.

Is there a simpler way of creating an index with the required structure (ViewA) given a collection of documents (DocA)?

FURTHER INFORMATION
The issue appears to be that in order to have the index hold the data in the transformed structure (ViewA), we have to do a Reduce. It appears that a Reduce must have both a GROUP ON and a SELECT in order to work as expected so the following are not valid:

INVALID REDUCE CLAUSE 1:

        Reduce = results => from result in results
                      group on new ViewA
                      {
                          Id = result.Id,
                          Name = result.Name,
                          CurrencyName = result.CurrencyName
                      } into g
                      select g.Key;

This produces: System.InvalidOperationException: Variable initializer select must have a lambda expression with an object create expression

Clearly we need to have the ‘select new’.

INVALID REDUCE CLAUSE 2:

        Reduce = results => from result in results
                      select new ViewA
                      {
                          Id = result.Id,
                          Name = result.Name,
                          CurrencyName = result.CurrencyName
                      };

This prduces: System.InvalidCastException: Unable to cast object of type ‘ICSharpCode.NRefactory.Ast.IdentifierExpression’ to type ‘ICSharpCode.NRefactory.Ast.InvocationExpression’.

Clearly, we also need to have the ‘group on new’.

Thanks for any assistance you can provide.

(Note: removing the type (ViewA) from the constructor calls has no effect on the above)

UPDATE WITH CORRECT APPROACH

As outlined in Daniel’s blog mentioned in the answer below, here is the correct way to do this for this example:

public class A_View : AbstractIndexCreationTask<DocA, ViewA>
{
    public A_View()
    {
        Map = docs => from doc in docs
                      select new ViewA
                      {
                          Id = doc.Id,
                          Name = doc.Name,
                          CurrencyName = doc.Currency.Name
                      };

        // Top-level properties on ViewA that match those on DocA
        // do not need to be stored in the index.
        Store(x => x.CurrencyName, FieldStorage.Yes);
    }
}
  • 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-13T20:12:04+00:00Added an answer on June 13, 2026 at 8:12 pm

    One solution, simply flatten in the Map and configure the index to store only properties that do not exist in DocA.

    public class A_View : AbstractIndexCreationTask<DocA, ViewA>
    {
        public A_View()
        {
            Map = docs => from doc in docs
                          select new ViewA
                          {
                              Id = doc.Id,
                              Name = doc.Name,
                              CurrencyName = doc.Currency.Name
                          };
    
            // Top-level properties on ViewA that match those on DocA
            // do not need to be stored in the index.
            Store(x => x.CurrencyName, FieldStorage.Yes);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following classes: public class Nation { public string Name { get; set;
Given the following classes: class Department { public String Name { get; set; }
Given the classes: public class Person { public string Name { get; set; }
Imagine the following classes: class A { public string Test {get; set;} } class
Given the following classes: public class User { public int Id {get;set;} public PersonName
Given the following POCO classes: public class Certification { public int Id { get;
Given the following assemblage of classes (contrived): public class School { [PrimaryKey] public string
I have the following (simplified) classes: public class Person { public string Id {get;
Given the following (simplified) classes: public class Master { public int Id { get;
Updated question given Andrew Hare's correct answer: Given the following C# classes: public class

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.