How to get max value of a column using Entity Framework?
Try this int maxAge = context.Persons.Max(p => p.Age);
And make sure you have using System.Linq;
at the top of your file
Return record with max column value using Entity Framework 6
You can simplify like the following using OrderByDescending
:
using (var db = new SystemEntities())
{
var record = db.Table.Where(p => p.Id == Id).OrderByDescending(x => x.ReceivedDateTime).FirstOrDefault();
if(record != null){}
}
How to select the maximum value of a column and retrieve multiple results?
So, for a given Computer
, the ICollection<Computer_Win_Installed_Software> Computer_Win_Installed_Software
is always going to include all related rows with all EntryTimestampts
. You can't (easily) filter the entities included.
So, instead, just return the filtered entities:
var computer = ComputersDbContext.Computers
.Where(a => a.ComputerId == computerId)
.First(t => t.TenantId == tenantId);
var cwis = computer
.Computer_Win_Installed_Software
.Where(b => b.EntryTimestamp == EntryTimestamp);
Of course, I'm not sure this is what you actually want to do, you may have an XY Problem.
Can you try not including the related objects, but just querying against them?
var computer = ComputersDbContext.Computers
.Where(a => a.ComputerId == computerId)
.First(t => t.TenantId == tenantId);
.Where(b => b.Computer_Win_Installed_Software.EntryTimestamp == EntryTimestamp);
EFCore Linq: Select only the records with the max value of a column
Group by won't work (no natural SQL translation).
However some sort of self join will do.
For instance
db.Runs.Where(e => e.Run == db.Runs.Max(e2 => (int?)e2.Run))
i.e. take the records with max Run
(see How to translate this SQL query to a LINQ query in EF Core? for why nullable cast is needed)
Or
db.Runs.Where(e => !db.Runs.Any(e2 => e2.Run > e.Run))
i.e. take records where no record with greater Run
exists.
How to get max value of a string column of numbers in Entity Framework
The error you get is because EF is trying to translate your linq query to actual SQL statements, but it can't.
What you could do is bring the data locally by forcing the query to execute, and then you will be able to convert to Int64 and check for Max.
Something like this:
LastInvoiceNo = dbContext.InvoiceMaster.ToList().Max(e => Convert.ToInt64(e.InvoiceNo));
Not sure if that's the best thing to do if you have millions of rows in that table though.
Cheers
How to return max value by asp.net mvc
You are looking for the Max()
method. Also use lambda expressions:
var max = context.TableName.Max(i => i.ColumnName);
(Where ColumnName is the column what you aggregate on.)
Getting max value on server (Entity Framework)
This can be addressed by typing the return as nullable so that you do not get a returned error and then applying a default value for the int. Alternatively, you can just assign it to a nullable int. Note, the assumption here of an integer return type of the ID. The same principal would apply to a Guid as well.
int MaxMessID = dbMeasuringsExisting.Max(p => (int?)p.MessID) ?? 0;
There is no need for the Any() statement as that causes an additional trip to the database which is not desirable in this case.
How to get max value from one table, then join another table using Entity Framework 6
if you have list of object with property
Id,Symbol,Price,LastUpdatedDate
let say
List<ItemPrice> itemPrice
then you can do like this
var lastUpdated= from item in itemPrice
group item by item.Symbol into itemGroup
let maxDate = itemGroup.Max(gt => gt.LastUpdatedDate)
select new RegistrationModel
{
Symbol = itemGroup.Key,
LastUpdatedDate= maxDate,
Price= itemGroup.First(gt2 => gt2.ExpiryDateAD == maxDate).Price
}).ToList()
i hope it may help you
Related Topics
How to Convert Image to Data Uri for HTML with C#
C# Compiler Bug? Why Doesn't This Implicit User-Defined Conversion Compile
Linq to Entities Generated SQL
How to Make the .Net Httpclient Use Http 2.0
Calling an Overridden Method from a Parent Class Ctor
How to Convert Code from C# to PHP
String.Format Alternative in C++
Change the Bordercolor of the Textbox
ASP.NET MVC 4 + Ninject MVC 3 = No Parameterless Constructor Defined for This Object
Onchange Event for HTML.Dropdownlist
Register Iauthenticationmanager with Simple Injector
Check If a String Is a Palindrome
How to Call a Function of a C++ Dll That Accepts a Parameter of Type Stringstream from C#
Difference Between SQLdatareader.Read and SQLdatareader.Nextresult