[dba-VB] Using aggregate SQL functions (Count(). Max(), ...) in ADO.NET EF queries in .NET Frame

Gustav Brock Gustav at cactus.dk
Wed Jun 30 09:40:49 CDT 2010


Hi Shamil

That was easy! 
Thanks for the feedback.

>>> shamil at smsconsulting.spb.ru 30-06-2010 12:21 >>>
Hi Gustav --

I've got it finally - it's easy as expected:

using (MyTestModel.MyTestEntities context = new
MyTestModel.MyTestEntities())
{
    var query = from aTable in context.myTable select aTable.ID;

    int maxId = query.Max();
    int minId = query.Min();

    System.Console.WriteLine("maxId = {0}, minId = {1}", maxId, minId); 
}

Thank you.

-- Shamil

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com 
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Shamil
Salakhetdinov
Sent: Wednesday, June 30, 2010 1:51 PM
To: 'Discussion concerning Visual Basic and related programming issues.'
Subject: Re: [dba-VB] Using aggregate SQL functions (Count(). Max(),...) in
ADO.NET EF queries in .NET Frame

Hi Gustav --

I just don't know how to write EF LINQ expression to get a Max ID value -
let's say Max ID for Order entity (Order.OrderId) - I guess it should be
easy...

Thank you.

-- Shamil

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com 
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Gustav Brock
Sent: Wednesday, June 30, 2010 12:45 PM
To: dba-vb at databaseadvisors.com 
Subject: Re: [dba-VB] Using aggregate SQL functions (Count(). Max(),...) in
ADO.NET EF queries in .NET Frame

Hi Shamil

So what you tell is that no ".Max()" is at hand or usable in your
expression? Or am I missing something?

/gustav


>>> shamil at smsconsulting.spb.ru 30-06-2010 07:47 >>>
Hi Gustav --

Thank you for your advice.
Still I have had to use ORDER BY ... DESC to find the last record (this is a
one user application with MS SQL backend) - I cannot find what EF LINQ
expression should be used to get max ID value - I still have to learn a lot
about EF LINQ here...

-- Shamil

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com 
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Gustav Brock
Sent: Friday, June 25, 2010 8:24 PM
To: dba-vb at databaseadvisors.com 
Subject: Re: [dba-VB] Using aggregate SQL functions (Count(). Max(),...) in
ADO.NET EF queries in .NET Framework

Hi Shamil

I have nothing running at the moment on this computer, but isn't it similar
to this (from examples in LinqPad):

For example, the following query will work on the NORTHWND sample database:

from p in Products
let spanishOrders = p.OrderDetails.Where (o => o.Order.ShipCountry == "Spain")
where spanishOrders.Any()
orderby p.ProductName
select new
{
	p.ProductName,
	p.Category.CategoryName,
	Orders = spanishOrders.Count(),	
	TotalValue = spanishOrders.Sum (o => o.UnitPrice * o.Quantity)
}

or as shown here:

http://msdn.microsoft.com/en-us/library/cc716738.aspx 

/gustav


>>> shamil at smsconsulting.spb.ru 25-06-2010 11:21 >>>
Hi All,

I can't find is the subj available or not by using LINQ for Entities?
If it's not available do you have good samples' references on how to use
ObjectQuery<...> with custom SQL using aggregare SQL functions?...

Thank you.

-- Shamil






More information about the dba-VB mailing list