How to get the maximum value of a string column of numbers in Entity Framework

I have a problem. I tried to get the maximum sql field value as

LastInvoiceNo = Convert.ToInt64(dbContext.InvoiceMaster.Max(e => e.InvoiceNo));

But that gives me the wrong value, since the column type "InvoiceNo" is varchar. So I tried to convert it to Int64 as

LastInvoiceNo = dbContext.InvoiceMaster.Max(e =>Convert.ToInt64(e.InvoiceNo));

and

LastInvoiceNo = dbContext.InvoiceMaster.Select(e => Int64.Parse(e.InvoiceNo)).Max();

But it throws an exception that

LINQ to Entities does not recognize the "Int64 Parse (System.String)" method, and this method cannot be translated into a repository expression.

+3
source share
2 answers

The error you are getting is that EF is trying to translate your linq query into actual SQL statements, but it cannot.

, , , Int64 Max.

- :

LastInvoiceNo = dbContext.InvoiceMaster.ToList().Max(e => Convert.ToInt64(e.InvoiceNo));

, , , .

+3

MSSQL, SqlFunctions, LINQ . , :

    dbContext.InvoiceMaster.Max(e =>SqlFunctions.Replicate("0", 16-e.InvoiceNo.Length) + e.InvoiceNo);

, , - PadLeft, SqlFunctions, , , , .

0

All Articles