How to find the highest version of varchar in c #?

I am trying to get the latest version of a program in C #. The code below is the only way I was able to get the code to run in my controller without errors:

PList = PList.Where(x => x.PSP == LP && x.VERSION == "1.3"); 

The problem is that if I do not support this code through each new version and do not fix it in the code for the latest version, it will not work correctly.

So, I thought that I could use the maximum function to get the latest version, but this is varchar in SQL DB, and I can’t change it, since I don’t have access to change types.

I tried different ways to do this, as an example below, with no luck.

 PList = PList.Where(x => x.PSP == LP && x.VERSION.MAX()); 

The && operator cannot be applied to operands of type "bool" and "char"

So, how can I get the Max version when I need to know if the PSP LP matches And is this the highest version in the database? Thanks for the help!

Update

Thanks to everyone for the excellent answers, and I see how some of them can work independently, but none of them worked with the code that I have. I think publishing the rest of the code can help make diagnosing the problem a little easier, so here is the rest of the code.

 public virtual ActionResult getAjaxP(string TP = null) { if (TP != null) { var PList = from x in db.iamp_mapping select x; PList = PList.Where(x => x.PSP == TP); return Json(PList.Select(x => new { x.PS }).Distinct().ToArray(), JsonRequestBehavior.AllowGet); } return View(); } 
+4
source share
3 answers

You can use the System.Version class.

 var maxVersion = PList .Select(x => new Version(x.VERSION)) .Max() .ToString(); PList = PList.Where(x => x.PSP == LP && x.VERSION == maxVersion); 

This will mean, for example, β€œ1.11” over β€œ1.3”.

If it is Linq-to-Sql, it may include an additional call to the database.

+4
source

Unfortunately, your version numbers are stored as text. This means that β€œ1.11” is actually earlier than β€œ1.3”.

Alternatively, you can use OrderByDescending . for instance

 var item = PList.Where(x => x.PSP == LP) .OrderByDescending(x => x.VERSION) .First(); 

This will get the only item with the highest version number. If there are several elements corresponding to this version number, it will be a little more complicated - you probably want to group by version, and then order using a group key. Basically, we need more information about what you are trying to achieve.

+4
source

try it

  var Max_VERSION = (from x in PList where x.PSP == LP select x.VERSION).Max(); 

A simple example:

 string[] versions = {"1.3","1.8","4.1","1.9", "1.4", "0.5", "1.7" }; var max_VeRsion = (from p in versions select p).Max(); 

oops:

  string[] versions = {"1.3","1.8","4.2","1.9", "4.11", "0.5", "1.17" }; var mv = (from p in versions let m = versions.Max().Split(Convert.ToChar(".")).First() let n = (from q in versions where q.Split(Convert.ToChar(".")).First() == m select Convert.ToInt32(q.Split(Convert.ToChar(".")).Last())).Max().ToString() where p.Split(Convert.ToChar(".")).First() == m && p.Split(Convert.ToChar(".")).Last() == n select p).First(); 

Last try:)

  string max_version_work = (from q in (from p in versions select new { Ma = Convert.ToInt32(p.Split('.').First()) , Mi = Convert.ToInt32(p.Split('.').Last()) }) orderby q.Ma descending, q.Mi descending select string.Format("{0}.{1}", q.Ma, q.Mi)).FirstOrDefault(); 
+1
source

All Articles