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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:42:33+00:00 2026-06-14T07:42:33+00:00

I have a document like this: order : 1 event : { timestamp: 1/1/2012,

  • 0

I have a document like this:

 order : 1
     event : { timestamp: 1/1/2012, employeeName: "mick" },
     event : { timestamp: 1/1/2012, employeeName: "mick" },
     event : { timestamp: 1/2/2012, employeeName: "rick" },
     event : { timestamp: 1/3/2012, employeeName: "mick" }

  order : 2
     event : { timestamp: 1/2/2012, employeeName: "mick" },
     event : { timestamp: 1/2/2012, employeeName: "rick" }

I want to run a map-reduce query to return a list of results grouped by date with a count of employee events per order.

In this case Mick had 2 events on the 1/1 on a single order. All the other days had a single event by employee on each order on the 2 and 3 of november. So i need a MAP function with results that would look like:

{ orderId: 1, date: 1/1/2012, employee: "mick", orderEventsCount: 2 },
{ orderId: 1, date: 1/2/2012, employee: "rick", orderEventsCount: 1 },
{ orderId: 2, date: 1/2/2012, employee: "mick", orderEventsCount: 1 },
{ orderId: 2, date: 1/2/2012, employee: "rick", orderEventsCount: 1 },
{ orderId: 1, date: 1/3/2012, employee: "mick", orderEventsCount: 1 }

Then i need a REDUCE function that will take these results and group by Date only and return a count per day of employees with multiple events on a single order:

{ date: 1/1/2012, multipleEventsPerOrdercount: 1 },
{ date: 1/2/2012, multipleEventsPerOrdercount: 0 },
{ date: 1/3/2012, multipleEventsPerOrdercount: 0 }

Since Mick was the only employee to have multiple events on a single date on a single order the result returned only a count of one employee with multiple events on an order on a date.

What would be the best way to write this map-reduce Raven query using LINQ in .NET?

Thanks

  • 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-14T07:42:34+00:00Added an answer on June 14, 2026 at 7:42 am

    Assuming your classes look like this:

    public class Order
    {
      public string Id  { get; set; }
      public List<Event> Events { get; set; }
    }
    
    public class Event
    {
      public DateTime Timestamp { get; set; }
      public string EmployeeName { get; set; }
    }
    

    Then the index you are asking for would look like this:

    public class Orders_EventCountsByDate : 
        AbstractIndexCreationTask<Order, Orders_EventCountsByDate.Result>
    {
      public class Result
      {
        public DateTime Date { get; set; }
        public double Count { get; set; }
      }
    
      public Orders_EventCountsByDate()
      {
        Map = orders => from order in orders
                        from evt in order.Events
                        let subtotal = order.Events.Count(x => x.EmployeeName == evt.EmployeeName && x.Timestamp == evt.Timestamp)
                        select new
                        {
                          evt.Timestamp.Date,
                          Count = subtotal > 1 ? (1.0 / subtotal) : 0
                        };
    
        Reduce = results => from result in results
                            group result by result.Date
                            into g
                            select new
                            {
                              Date = g.Key,
                              Count = g.Sum(x => x.Count)
                            };
      }
    }
    

    And you would use it like this:

    var counts = session.Query<Orders_EventCountsByDate.Result,
                               Orders_EventCountsByDate>();
    

    The trick here is that you are making a determination in the Map about how much you want each event to contribute to the count. If there is only one event, you contribute zero. When there are multiple events, each event contributes a fraction of the total. These fractions are later summed up in the reduce, returning you to near whole numbers. The double floating point math should get you back to whole numbers, but you still may want to round to the nearest whole integer in your client side code just to be safe.

    This also assumes that all events are in the same timezone and you don’t care about daylight savings changes, or the times are in UTC. If neither, then you should use DateTimeOffset and you have more to consider when deciding what each employee’s concept of a Day is.

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

Sidebar

Related Questions

I have a XML document which looks something like this: <events> <event category=gymnastics subcategory=rhythmic
i have a document like this: English English English English 中文中文中文中文中文 English English English
Imagine I have a document like this: post = {title: a, comments: [{hour: 08:00,
I have a xml document like this : <Node1 attrib1=abc> <node1_1> <node1_1_1 attrib2 =
Hi I have an xml document like this <root> <cert id=1> </cert> <cert id=2>
I have an xml document that looks like this. <foo> <bar type=artist/> Bob Marley
I have a document, something like this: <root> <A node=1/> <B node=2/> <A node=3/>
I have an XML document which looks something like this: <meadinkent> <record> <comp_div>MENSWEAR</comp_div> <sty_ret_type>ACCESSORIES</sty_ret_type>
So I have a DOM document that looks essentially like this <categories> <category id=1/>
I have an XML document that matches our site navigation something like this: <page

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.