473,382 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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.Transaction
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
Nov 14 '08 #1
4 5311
On Thu, 13 Nov 2008 21:14:58 -0800, Paolo
<Pa***@discussions.microsoft.comwrote:
[...]
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
Nov 14 '08 #2
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
<Pa***@discussions.microsoft.comwrote:
[...]
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
Nov 14 '08 #3
On Thu, 13 Nov 2008 21:54:23 -0800, Paolo
<Pa***@discussions.microsoft.comwrote:
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
Nov 14 '08 #4
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
<Pa***@discussions.microsoft.comwrote:
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
Nov 14 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Greg Stark | last post by:
I find I often want to be able to do joins against views where the view are aggregates on a column that has an index. Ie, something like SELECT a.*, v.n FROM a JOIN (select a_id,count(*) as n...
2
by: Toby Dann | last post by:
I have an aggregate query as recordsource for a form to show a list of invoices, including the totals (calculated by the aggregate query - I'm trying to duplicate as little info as possible here)....
0
by: BillCo | last post by:
just wasted a long time figuring out this and I figure if I post it might save someone some pain! Jet (DAO) will allow you to to use nested aggregate functions like building blocks, e.g.: ...
5
by: BillCo | last post by:
I just wasted a long time figuring out this and I figure if I post it might save someone some pain! Jet (DAO) will allow you to to use nested aggregate functions like building blocks, e.g.: ...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
0
by: =?Utf-8?B?SHlwZXJjb2Rlcg==?= | last post by:
I'm encountering some strange behavior after deploying a ASP.net 3.5 website to production, i'm unable to reproduce these in my dev environment. This error seems to occur very randomly but it's...
14
by: thj | last post by:
Hi, I was wondering what you guys are using and why? LINQ to SQL or NHibernate? Thanks in advance, Tommy
4
by: shapper | last post by:
Hello, I have the following Linq query: var q = (from p in database.Posts join pt in database.PostsTags on p.PostID equals pt.PostID join t in database.Tags on pt.TagID equals t.TagID group...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.