Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
GroupBy and OrderBy in MultiMap/Reduce
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Inveritas  
View profile  
 More options May 7 2012, 8:40 am
From: Inveritas <maso...@gmail.com>
Date: Mon, 7 May 2012 05:40:29 -0700 (PDT)
Local: Mon, May 7 2012 8:40 am
Subject: GroupBy and OrderBy in MultiMap/Reduce
Hi Mailing list,

I have following reduce in a MultiMap/Reduce:

            Reduce = results => from result in results
                                group result by result.GroupId
                                into g
                                select new
                                {
                                    GroupId = g.Key,
                                    Contacts= g.Select(x =>
x.Contacts).FirstOrDefault(x => x != null),
                                    Subject = g.Select(x =>
x.Subject).FirstOrDefault(x => x != null),
                                    Text = g.Select(x =>
x.Text).FirstOrDefault(x => x != null),
                                    SendDate = g.Select(x =>
x.SendDate).FirstOrDefault(x => x != null),
                                };

..and it returns this aggregated Message:

{
  "GroupId": "groups/1505",
  "Contacts": [
    "f...@test.com",
    "t...@test.com"
  ],
  "Subject": "Latest message",
  "Text": "This should be my latest message",
  "SendDate": "20120505220951000"

}

Now I need to get the latest Message in each Group. I guess I need to
OrderByDescending on SendDate before the actual aggregation is done. I
have tried order by in some places but it I will not work, just get an
empty result.

Thankful For Any Help.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ryan Heath  
View profile  
 More options May 7 2012, 9:17 am
From: Ryan Heath <ryan.q.he...@gmail.com>
Date: Mon, 7 May 2012 15:17:27 +0200
Local: Mon, May 7 2012 9:17 am
Subject: Re: [RavenDB] GroupBy and OrderBy in MultiMap/Reduce
How about something like:

Reduce = results => from result in results
                               group result by result.GroupId
                               into g
let firstMessage = (from m in g order by m.SendDate).First()
let lastMessage = (from m in g order by m.SendDate desc).First()
                               select new
                               {
                               ...
                               // use first and last messages
                               ...
                               };

// Ryan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Inveritas  
View profile  
 More options May 7 2012, 9:48 am
From: Inveritas <maso...@gmail.com>
Date: Mon, 7 May 2012 06:48:51 -0700 (PDT)
Local: Mon, May 7 2012 9:48 am
Subject: Re: GroupBy and OrderBy in MultiMap/Reduce
That looks like it should work. I've changed my code according to
yours but still get empty result, here is my Query:

            Reduce = results => from result in results
                                group result by result.GroupId
                                into g
                                let latestMessage = (from m in g
orderby m.SendDate descending select m).FirstOrDefault()
                                select new
                                {
                                    GroupId = g.Key,
                                    Emails = latestMessage.Emails,
                                    Contacts = latestMessage.Contacts,
                                    Subject = latestMessage.Subject,
                                    IntroText =
latestMessage.IntroText,
                                    SendDate = latestMessage.SendDate
                                };

Have I missed something here?

/Mase

On 7 Maj, 15:17, Ryan Heath <ryan.q.he...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Inveritas  
View profile  
 More options May 7 2012, 3:03 pm
From: Inveritas <maso...@gmail.com>
Date: Mon, 7 May 2012 12:03:07 -0700 (PDT)
Local: Mon, May 7 2012 3:03 pm
Subject: Re: GroupBy and OrderBy in MultiMap/Reduce
Finally it works! Here is the solution if anyone faces same issue:

            Reduce = results => from result in results
                                group result by result.GroupId
                                into g
                                select new
                                {
                                    GroupId = g.Key,
                                    Emails = g.Select(x =>
x.Emails).FirstOrDefault(x => x != null),
                                    Contacts = g.Select(x =>
x.Contacts).FirstOrDefault(x => x != null),
                                    Subject = g.Where(x => x.SendDate !
= null).OrderByDescending(x => x.SendDate).Select(x =>
x.Subject).FirstOrDefault(x => x != null),
                                    IntroText = g.Where(x =>
x.SendDate != null).OrderByDescending(x => x.SendDate).Select(x =>
x.IntroText).FirstOrDefault(x => x != null),
                                    SendDate = g.Where(x =>
x.SendDate != null).OrderByDescending(x => x.SendDate).Select(x =>
x.SendDate).FirstOrDefault(x => x != null)
                                };

/Masod

On 7 Maj, 15:48, Inveritas <maso...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »