Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > SQL Query to Linq (Group By and Sum)

Reply
Thread Tools

SQL Query to Linq (Group By and Sum)

 
 
Alex Sauceda
Guest
Posts: n/a
 
      02-04-2008
Hi Everyone,

I'm trying to convert this query into LINQ but I don't have idea how to do
it.

Select C.sName as CustomerName, S.sName as Salesperson,
Sum(O.nQty) as Qty, Sum(O.mAmount) as Amount
From tOrders O
Inner Join tCustomer C On C.CustomerID = O.CustomerID
Inner Join tSalesperson S On S.SalespersonID = O.SalespersonID
Group By C.sName, S.sName

I Hope you can help me.

Regards,

Alex

 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      02-04-2008
Well, it is hard to test without more info (perhaps something similar
on "pubs" or "northwind"?) - but perhaps something along the lines of:

var query = from order in db.Orders
group order by new { Customer = order.Customer,
SalesPerson = order.SalesPerson }
into grp
select new
{
Customer = grp.Key.Customer.Name,
SalesPerson = grp.Key.SalesPerson.Name,
Quantity = grp.Sum(o => o.Quantity),
Amount = grp.Sum(o => o.Amount)
};

foreach (var item in query)
{
Console.WriteLine("{0}, {1}, {2}, {3}", item.SalesPerson,
item.Customer, item.Amount, item.Quantity);
}
 
Reply With Quote
 
 
 
 
Alex Sauceda
Guest
Posts: n/a
 
      02-04-2008
Using Northwind it would be something like this:
Select C.CompanyName, O.ShipCity, Sum(D.Quantity) as Quantity,
Sum(D.Quantity*D.UnitPrice) as Amount
From Customers C Inner Join
Orders O On C.CustomerID = O.CustomerID Inner Join
[Order Details] D on O.OrderID = D.OrderID
Group by C.CompanyName, O.ShipCity
Order by C.CompanyName, O.ShipCity


Thanks a lot for helping me.


"Marc Gravell" <> wrote in message
news:29d78dac-5605-44cf-a8e7-...
> Well, it is hard to test without more info (perhaps something similar
> on "pubs" or "northwind"?) - but perhaps something along the lines of:
>
> var query = from order in db.Orders
> group order by new { Customer = order.Customer,
> SalesPerson = order.SalesPerson }
> into grp
> select new
> {
> Customer = grp.Key.Customer.Name,
> SalesPerson = grp.Key.SalesPerson.Name,
> Quantity = grp.Sum(o => o.Quantity),
> Amount = grp.Sum(o => o.Amount)
> };
>
> foreach (var item in query)
> {
> Console.WriteLine("{0}, {1}, {2}, {3}", item.SalesPerson,
> item.Customer, item.Amount, item.Quantity);
> }


 
Reply With Quote
 
Alex Sauceda
Guest
Posts: n/a
 
      02-04-2008
Your sample helped me a lot.

Thanks a lot Marc.

Regards,

Alex

"Marc Gravell" <> wrote in message
news:29d78dac-5605-44cf-a8e7-...
> Well, it is hard to test without more info (perhaps something similar
> on "pubs" or "northwind"?) - but perhaps something along the lines of:
>
> var query = from order in db.Orders
> group order by new { Customer = order.Customer,
> SalesPerson = order.SalesPerson }
> into grp
> select new
> {
> Customer = grp.Key.Customer.Name,
> SalesPerson = grp.Key.SalesPerson.Name,
> Quantity = grp.Sum(o => o.Quantity),
> Amount = grp.Sum(o => o.Amount)
> };
>
> foreach (var item in query)
> {
> Console.WriteLine("{0}, {1}, {2}, {3}", item.SalesPerson,
> item.Customer, item.Amount, item.Quantity);
> }


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      02-04-2008
> Your sample helped me a lot.

No problem; actually, it occurred to me that do get something close to
your original, you should probably group directly on the names, i.e.

group order by new { CustomerName = order.Customer.Name,
SalesPersonName = order.SalesPerson.Name }
into grp
select new
{
grp.Key.CustomerName,
grp.Key.SalesPersonName,
<etc as before>

For reference, you can inspect the generated SQL on a data-context via
GetCommand(query); or use a SQL trace.

Glad it helped, though ;-p
 
Reply With Quote
 
Alex Sauceda
Guest
Posts: n/a
 
      02-04-2008
SQL Trace helped me a lot.

Thanks a again for answering.

Regards,

Alex

"Marc Gravell" <> wrote in message
news:9c64409e-6ac0-4a88-abad-...
>> Your sample helped me a lot.

>
> No problem; actually, it occurred to me that do get something close to
> your original, you should probably group directly on the names, i.e.
>
> group order by new { CustomerName = order.Customer.Name,
> SalesPersonName = order.SalesPerson.Name }
> into grp
> select new
> {
> grp.Key.CustomerName,
> grp.Key.SalesPersonName,
> <etc as before>
>
> For reference, you can inspect the generated SQL on a data-context via
> GetCommand(query); or use a SQL trace.
>
> Glad it helped, though ;-p


 
Reply With Quote
 
Alex Sauceda
Guest
Posts: n/a
 
      02-04-2008
SQL Trace helped me a lot.

Thanks again for answering.

Regards,

Alex

"Marc Gravell" <> wrote in message
news:9c64409e-6ac0-4a88-abad-...
>> Your sample helped me a lot.

>
> No problem; actually, it occurred to me that do get something close to
> your original, you should probably group directly on the names, i.e.
>
> group order by new { CustomerName = order.Customer.Name,
> SalesPersonName = order.SalesPerson.Name }
> into grp
> select new
> {
> grp.Key.CustomerName,
> grp.Key.SalesPersonName,
> <etc as before>
>
> For reference, you can inspect the generated SQL on a data-context via
> GetCommand(query); or use a SQL trace.
>
> Glad it helped, though ;-p


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't run linq to sql query? Andy B. ASP .Net 4 09-08-2009 09:19 PM
DetailsView and foreign key and LINQ-to-SQL Craig Buchanan ASP .Net 0 01-16-2009 03:48 PM
Linq or not Linq George ASP .Net 4 11-05-2008 04:53 PM
LINQ to SQL and new MS SQL 2008 datatypes Mike Gleason jr Couturier ASP .Net 1 10-29-2008 03:42 AM
Build dynamic sql query for JSTL <sql:query> Anonymous Java 0 10-13-2005 10:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57