Well, the SQL Statement is complete, however, i think that it implies some grouping. Here is what I've worked up for grouping. It seems to like that and I'm working on testing the results, but does this look correct?
Main Topics
Browse All TopicsLet's say i have this query in SQL
Select InstNumber, AccountNumber, Sum(Amount) from Items..
How do i replicate that in LINQ?
I'm tried this below example and i can select the sum column, but cannot select my individual columns. I get the error "Cannot assign lambda expression to anonoymous type property.
See attached code for example
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi jpmc_cmsops;
To your statement, "Well, the SQL Statement is complete, however, i think that it implies some grouping", The SQL statement in the question, "Select InstNumber, AccountNumber, Sum(Amount) from Items", would cause an error in SQL Server because the SUM function requires an aggregate function or the GROUP BY clause which is not stated. For example the following would work:
SELECT InstNumber, AccountNumber, Sum(Amount)
FROM Items
GROUP BY InstNumber, AccountNumber
This Ling to SQL query in your last post will send the following T-SQL statement to the SQL Server.
var groupTest = (from item in Items
group item by
new { item.InstNumber, item.LogicalAccount} into g
select new
{
InstNumber = g.Key.InstNumber,
Account = g.Key.LogicalAccount,
FloatAmount = g.Sum(item => item.LogicalAmount)
}
);
T-SQL statement sent to the server.
SELECT SUM([t0].[LogicalAmount]) AS [FloatAmount], [t0].[InstNumber] AS [InstNumber],
[t0].[LogicalAccount] AS [Account]
FROM [dbo].[Items] AS [t0]
GROUP BY [t0].[InstNumber], [t0].[LogicalAccount]
Which will return the same result set as the T-SQL statement above. You can always see what is sent to SQL server by placing the following statement before executing a Linq query.
TheDataContextInstanceName
In the above the T-SQL statement will be written to the Console window.
Fernando
Thanks for the reply. In looking further at the SQL statement that I am trying to mimick with LINQ and to give a bit more context as to my real-world situation, the SQL statement was inside a SPROC and apparently InstNumber and AccountNumber are @variables obtained from other parts of the SPROC and the only thing actually selected is the Sum(Account). Running that by itself, it worked. when i added the additional selects, it indeed gave me the aggregate error.
Business Accounts
Answer for Membership
by: FernandoSotoPosted on 2009-11-10 at 20:21:17ID: 25792262
Hi jpmc_cmsops;
I am no SQL expert but this SQL statement does not seem to be complete:
Select InstNumber, AccountNumber, Sum(Amount) from Items
I would think that a grouping clause or where clause is needed. In your Linq to SQL query you are grouping the results on the table column Amount, so all the same values of Amount are grouped together no matter what its AccountNumber or InstNumber are. Is that what you want?
Fernando