| layout | default | |||
|---|---|---|---|---|
| sidebar | operations | |||
| title | Max | |||
| permalink | /operation/max | |||
| tags |
|
|||
| parent | OPERATIONS |
This method is used to compute the maximum value of the target field.
Below is the sample code that gets the maximum value of column Value from the [dbo].[Sales] table for a specific customer since yesterday.
using (var connection = new SqlConnection(connectionString))
{
var customerExpenses = connection.Max<Sales>(e => e.Value,
e => e.CustomerId == 10045 && e.DateInsertedUtc >= DateTime.UtcNow.Date.AddDays(-1));
}You can also target a specific table by passing the literal table and field name like below.
using (var connection = new SqlConnection(connectionString))
{
var customerExpenses = connection.Max("[dbo].[Sales]", Field.From("Value"), new { State = "Michigan" });
}Or, use the QueryGroup or QueryField if you are to enhance the WHERE expressions.
using (var connection = new SqlConnection(connectionString))
{
var where = new []
{
new QueryField("CustomerId", 10045),
new QueryField("DateInsertedUtc", Operation.GreaterThanOrEqual, DateTime.UtcNow.Date.AddDays(-1))
}
var customerExpenses = connection.Max("[dbo].[Sales]", Field.From("Value"), where: where);
}To pass a hint, simply write the table-hints and pass it in the hints argument.
using (var connection = new SqlConnection(connectionString))
{
var customerExpenses = connection.Max<Sales>(e => e.Value,
e => e.CustomerId == 10045 && e.DateInsertedUtc >= DateTime.UtcNow.Date.AddDays(-1),
hints: "WITH (NOLOCK)");
}Or, you can use the SqlServerTableHints class.
using (var connection = new SqlConnection(connectionString))
{
var customerExpenses = connection.Max<Sales>(
e => e.CustomerId == 10045 && e.DateInsertedUtc >= DateTime.UtcNow.Date.AddDays(-1),
hints: SqlServerTableHints.NoLock);
}