Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Aggregate, LINQ, c#

Posted on 2014-12-03
4
Medium Priority
?
295 Views
Last Modified: 2016-02-15
The following code finds a list of Quantity values ok.

Question: At line 2, how aggregate could be applied to this code to get total quantity?

            Dim s List(Of Order)= (From o In db.Orders Where o.ID = 2).ToList()

            Dim sum = From o In s.OrderItems Select Total = o.Quantity

Open in new window

0
Comment
Question by:Mike Eghtebas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 34

Expert Comment

by:it_saige
ID: 40479887
Are you looking for something like this:
Imports System.Linq

Module Module1
	Private orders As New List(Of Order) From _
	 { _
	  New Order() With {.ID = 1, .Quantity = 5}, _
	  New Order() With {.ID = 2, .Quantity = 12}, _
	  New Order() With {.ID = 3, .Quantity = 7}, _
	  New Order() With {.ID = 2, .Quantity = 2}, _
	  New Order() With {.ID = 1, .Quantity = 1}, _
	  New Order() With {.ID = 3, .Quantity = 8} _
	 }

	Sub Main()
		Console.WriteLine(String.Format("Quantity of Orders for OrderID 2 = {0}", (Aggregate order As Order In orders Where order.ID.Equals(2) Into Sum(order.Quantity))))
		Console.ReadLine()
	End Sub
End Module

Class Order
	Property ID() As Integer
	Property Quantity() As Integer
End Class

Open in new window


Produces the following output:Capture.JPG
-saige-
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40479923
Hi Saige,

Orders is in Entity Framework define by db and has data in it.

Dim db As New TestDatabaseEntities


I want to pull that data. If I were doing it in SQL:

Select Sum(o.Quantity) From tblOrders  o Where o.ID =2

or

Select o,ID, Sum(o.Quantity) From tblOrders  Group By o.ID Having ID =2
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 1200 total points
ID: 40479986
Hi eghtebas;

Try these out, it should give you what you are looking for.

'' Select Sum(o.Quantity) From tblOrders  o Where o.ID =2
Dim sumQuty = (From o In db.tblOrders
               Where o.ID = 2
               Select o.Quantity).Sum()


'' Select o,ID, Sum(o.Quantity) From tblOrders  Group By o.ID Having ID =2               
Dim sumQuty2 = From o In db.tblOrders
               Group o By ID = o.ID Into orderGroup = Group
               Where ID = 2
               Select ID, sumQuty = orderGroup.Sum(Function(q) q.Quantity)

Open in new window

0
 
LVL 34

Assisted Solution

by:it_saige
it_saige earned 800 total points
ID: 40479987
Well you should still be able to use Aggregate, try:
'  This is the list of orders.
Dim s List(Of Order)= (From o In db.Orders Where o.ID = 2).ToList()
'  So long as Quantity is a property of order, we can sum it.
Dim sum = (Aggregate o In s Into Sum(o.Quantity))

Open in new window


-saige-
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question