How to aggregate in a LINQ query

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?UGFvbG8=?=

    #1

    How to aggregate in a LINQ query

    I have a LINQ query which returns a sequence of rows all of which contains a
    decimal field (T_Amount) e.g.

    var trans =
    from trans in dataSet.Transac tion
    where .... (filters)
    ....
    select new
    {
    trans.T_Date,
    p.P_Name,
    c.C_Description ,
    s.S_Description ,
    pm.Value,
    trans.T_Amount <<<< want to sum on this
    };

    I want to sum the values in T_Amount and show it in a label on a windows form.

    I'd appreciate an example of how to construct such an aggregation, returning
    the
    total for sequence. I've done a fair amount of research but haven't found a
    workable example.

    Thanks
  • Peter Duniho

    #2
    Re: How to aggregate in a LINQ query

    On Thu, 13 Nov 2008 21:14:58 -0800, Paolo
    <Paolo@discussi ons.microsoft.c omwrote:
    [...]
    I want to sum the values in T_Amount and show it in a label on a windows
    form.
    >
    I'd appreciate an example of how to construct such an aggregation,
    returning the
    total for sequence. I've done a fair amount of research but haven't
    found a
    workable example.
    Did you look at the Enumerable.Sum< TSource>() method? Something like this:

    decimal sum = trans.Sum(t =t.T_Amount);

    Might do what you want.

    Pete

    Comment

    • =?Utf-8?B?UGFvbG8=?=

      #3
      Re: How to aggregate in a LINQ query

      Pete: Thanks - yes, I've been poring over the Enumerable class and decided
      Sum() was what I needed. I just didn't know how to express the code.

      Now, where do I put decimal sum = trans.Sum(t =t.T_Amount); ?

      If I put it after the select new {...}; statement I get "The name 'trans'
      does not exist in the current context".

      "Peter Duniho" wrote:
      On Thu, 13 Nov 2008 21:14:58 -0800, Paolo
      <Paolo@discussi ons.microsoft.c omwrote:
      >
      [...]
      I want to sum the values in T_Amount and show it in a label on a windows
      form.

      I'd appreciate an example of how to construct such an aggregation,
      returning the
      total for sequence. I've done a fair amount of research but haven't
      found a
      workable example.
      >
      Did you look at the Enumerable.Sum< TSource>() method? Something like this:
      >
      decimal sum = trans.Sum(t =t.T_Amount);
      >
      Might do what you want.
      >
      Pete
      >

      Comment

      • Peter Duniho

        #4
        Re: How to aggregate in a LINQ query

        On Thu, 13 Nov 2008 21:54:23 -0800, Paolo
        <Paolo@discussi ons.microsoft.c omwrote:
        Pete: Thanks - yes, I've been poring over the Enumerable class and
        decided
        Sum() was what I needed. I just didn't know how to express the code.
        >
        Now, where do I put decimal sum = trans.Sum(t =t.T_Amount); ?
        >
        If I put it after the select new {...}; statement I get "The name 'trans'
        does not exist in the current context".
        After the semi-colon? If so, then you didn't post the actual code you're
        using. The variable "trans" should just be whatever you've assigned the
        query result to. Assuming you've actually named it something other than
        what you showed in your first post (which seems reasonable...I wouldn't
        actually use the same name inside the query as I use for the results),
        then you need to replace the "trans" in the statement I posted with
        whatever the actual variable name is.

        Pete

        Comment

        • =?Utf-8?B?UGFvbG8=?=

          #5
          Re: How to aggregate in a LINQ query

          Pete: thanks again. My original query variable was qryTrans. I must have
          missed off the 'qry' bit when I cut and pasted the code. Anyway I've made
          the change and it works perfectly.

          "Peter Duniho" wrote:
          On Thu, 13 Nov 2008 21:54:23 -0800, Paolo
          <Paolo@discussi ons.microsoft.c omwrote:
          >
          Pete: Thanks - yes, I've been poring over the Enumerable class and
          decided
          Sum() was what I needed. I just didn't know how to express the code.

          Now, where do I put decimal sum = trans.Sum(t =t.T_Amount); ?

          If I put it after the select new {...}; statement I get "The name 'trans'
          does not exist in the current context".
          >
          After the semi-colon? If so, then you didn't post the actual code you're
          using. The variable "trans" should just be whatever you've assigned the
          query result to. Assuming you've actually named it something other than
          what you showed in your first post (which seems reasonable...I wouldn't
          actually use the same name inside the query as I use for the results),
          then you need to replace the "trans" in the statement I posted with
          whatever the actual variable name is.
          >
          Pete
          >

          Comment

          Working...