none
Return just a record count RRS feed

  • Question

  • I am building an XML form from a LINQ query in the single step process and want to add a node called <Count> to each group.  I am using the .count() method and it returns {Message type=N, dm.count = 2}.  It is placed after a groupby() method.  I would like to return only the integer (I can cast from string if necessary) and not the key name.  Here is what my query returns and below that is the LINQ code snippet which created it:
    RETURN:
      <?xml version="1.0" encoding="utf-8" ?>
    - <Messages>
      <nextForm>MemberMenu.xml</nextForm>
      <thisForm>MessageData.xml</thisForm>
    - <NMessages>
    - <Message>
      <messageID>3f018ffd-e4b7-4ac0-ad2f-a182a628cd32</messageID>
      <deliveredtime>2009-03-25T08:56:43.727</deliveredtime>
      <sentfrom>null</sentfrom>
      </Message>
      <Ncount>{ MessageType = N, MessageCount = 1 }</Ncount>
      </NMessages>
    - <SMessages>
    - <Message>
      <messageID>3f018ffd-e4b7-4ac0-ad2f-a182a628cd39</messageID>
      <deliveredtime>2009-06-08T00:00:00</deliveredtime>
      <sentfrom />
      </Message>
      <Scount>{ MessageType = S, MessageCount = 1 }</Scount>
      </SMessages>
    - <DMessages>
    - <Message>
      <messageID>3f018ffd-e4b7-4ac0-ad2f-a182a628cd42</messageID>
      <deliveredtime>2009-06-10T09:00:36</deliveredtime>
      <sentfrom />
      </Message>
      <Dcount>{ MessageType = D, MessageCount = 1 }</Dcount>
      </DMessages>
      </Messages>

    HERE'S THE CODE SNIPPET:
       new XElement("SMessages",
                    from smessage in dc.Messages
                    orderby smessage.ReceivedTime
                    where smessage.MsgType == 'S' && smessage.UserID == ID && smessage.CompanyID == CompanyID
                    select new XElement("Message",
                            new XElement("messageID", smessage.MessageID),
                            new XElement("deliveredtime", smessage.ReceivedTime),
                            new XElement("sentfrom", smessage.SentFrom)),
                            new XElement("Scount",
                                from message in dc.Messages
                                where message.MsgType == 'S' && message.UserID == ID && message.CompanyID == CompanyID
                                group message by message.MsgType into sm
                                select new { MessageType = sm.Key, MessageCount = sm.Count() })),
    Friday, June 12, 2009 2:19 PM

Answers

  • It looks like you want this:

      new XElement("SMessages",
                    from smessage in dc.Messages
                    orderby smessage.ReceivedTime
                    where smessage.MsgType == 'S' && smessage.UserID == ID && smessage.CompanyID == CompanyID
                    select new XElement("Message",
                            new XElement("messageID", smessage.MessageID),
                            new XElement("deliveredtime", smessage.ReceivedTime),
                            new XElement("sentfrom", smessage.SentFrom)),
                            new XElement("Scount",
                                dc.Messages.Count (m => m.MsgType == 'S' && m.UserID == ID && m.CompanyID == CompanyID) )),

    Joe
    Write LINQ queries interactively - www.linqpad.net
    • Marked as answer by mreynol6 Friday, June 12, 2009 3:51 PM
    Friday, June 12, 2009 2:37 PM
    Answerer

All replies

  • It looks like you want this:

      new XElement("SMessages",
                    from smessage in dc.Messages
                    orderby smessage.ReceivedTime
                    where smessage.MsgType == 'S' && smessage.UserID == ID && smessage.CompanyID == CompanyID
                    select new XElement("Message",
                            new XElement("messageID", smessage.MessageID),
                            new XElement("deliveredtime", smessage.ReceivedTime),
                            new XElement("sentfrom", smessage.SentFrom)),
                            new XElement("Scount",
                                dc.Messages.Count (m => m.MsgType == 'S' && m.UserID == ID && m.CompanyID == CompanyID) )),

    Joe
    Write LINQ queries interactively - www.linqpad.net
    • Marked as answer by mreynol6 Friday, June 12, 2009 3:51 PM
    Friday, June 12, 2009 2:37 PM
    Answerer
  • Many thanks, Joe.  It works like a charm, but you already knew that :-)
    Friday, June 12, 2009 3:51 PM