简体   繁体   中英

C# Get MAX value

Is this the correct way to get the MAX value (id) from the database and then substrat 6 to it?

var photoso = db.QuerySingle("SELECT MAX(Id) FROM Photos");
var toto = photoso - 6;

It is telling me that I can not use a '-' opperator between a WebMatrix.Data.DynamicRecord and Int

Your query should be

var photoso = db.QueryValue("SELECT MAX(Id) FROM Photos");
var toto = photoso - 6;

Look at the difference between QuerySingle and QueryValue in the Database class

photoso would return a collection of data to your. To get one object, use its name in the Collection. Just like accessing an object in Array or List using its Index number.

You code is as

            photoso           -   6
WebMatrix.Data.DynamicRecord and Int

Try this code instead

var photoso = db.QueryValue("SELECT MAX(Id) FROM Photos");
var toto = (photoso - 6); // get the column Id from the Collection data

This would work.

Here is a tutorial for that by Mike Brind: http://www.mikesdotnetting.com/Article/214/How-To-Check-If-A-Query-Returns-Data-In-ASP.NET-Web-Pages

Hover over the var keyword for photoso .

It may not be an int so you would need to cast the returned max value to an int and then subtract 6.

Yes, that query seems fine, but that error would suggest that there is a type mismatch.

I would try extracting the value from photoso , so that it is an int , or casting photoso as an int. Try using photoso.Id or similar property to get the numerical value out of the photoso query result.

Of course, that's assuming that subtracting 6 from the resulting Id will still be valid, and that Id is a numerical value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM