简体   繁体   English

Linq选择ID为字母数字的最高ID

[英]Linq to select highest ID where the ID is alphanumeric

I have a list of ID where the ID's start with MB (for members) or NM (for non members). 我有一个ID列表,其中ID以MB(对于成员)或NM(对于非成员)开头。

The ID would be like MB-101 or NM-108 etc... 该ID类似于MB-101或NM-108等...

I am trying to select the Highest ID starting with MB and then Add 1 and then save back to DB. 我试图选择以MB开头的最高ID,然后再添加1,然后再保存回DB。 Saving is easy but I am not sure how to query the highest Member or Nonmember ID and add one to it. 保存很容易,但是我不确定如何查询最高的Member或Nonmember ID并将其添加一个。 Any help is much appreciated 任何帮助深表感谢

You can do something like this: 您可以执行以下操作:

List<string> list = new List<string>() { "MB-101", "MB-102", "MB-103", "MB-104"};
var ids = list.Select(x =>Convert.ToInt32(x.Replace("MB-", "")));//convert all the number parts to integer
list[list.FindIndex(x => x == "MB-" + ids.Max().ToString())] = "MB-" + (ids.Max() + 1);//set the max number after adding one.

You can do the same with your Nonmember ID. 您可以使用非会员ID进行相同的操作。 It is tested code, it successfully addresses your problem. 它是经过测试的代码,可以成功解决您的问题。

Hope it helps. 希望能帮助到你。

You can get the max id by splitting your list like below: 您可以通过如下拆分列表来获取最大ID:

var ids = yourList.Where(x => x.ID.StartsWith("MB-")).Select(x => new { ID = x.ID.Split('-')[1] }).ToList();
var maxIdValue = ids.Select(x => int.Parse(x.ID)).ToList().Max();

If you want max id from both starting with MB- and NB- than you can remove above where condition. 如果您想同时使用MB-和NB-开头的最大ID,则可以在where条件以上删除。 By this it will fetch max id from both MB- and NB-. 这样,它将同时从MB-和NB-获取最大ID。 Following will be query than: 查询以下内容:

var ids = yourList.Select(x => new { ID = x.ID.Split('-')[1] }).ToList();
var maxIdValue = ids.Select(x => int.Parse(x.ID)).ToList().Max();

you can try like this 你可以这样尝试

List<string> lststr = new List<string>() { "MB-101", "MB-103", "MB-102", "NM-108" };

var result = "MB-"+ lststr
            .Where(x=>x.Contains("MB"))
            .Select(x => Regex.Replace(x, @"[^\d]", ""))
            .OrderByDescending(x=>x)
            .FirstOrDefault();

it will return MB-103 because it will first check if the string contains MB then it will replace everything with "" other than digit and OrderByDescending it will order by Descending so that the highest value will be on top and at last FirstOrDefault will get the fist value 它将返回MB-103因为它将首先检查字符串是否包含MB然后它将用""而不是数字)替换所有内容, OrderByDescending降序对OrderByDescending进行排序,以使最大值位于顶部,最后FirstOrDefault将获得拳头价值

You have to do OrderByDescending your list by replacing MB or NM with empty and get FirstOrDefault from ordered list. 您必须通过将MBNM替换为空来执行OrderByDescending列表,并从有序列表中获取FirstOrDefault Please check below example. 请检查以下示例。

CODE: 码:

List<string> list = new List<string>() { "MB-101", "MB-102", "MB-109", "MB-105", "NM-110"};
var maxMember = list.OrderByDescending(m=>Convert.ToInt16(m.Replace("MB-","").Replace("NM-",""))).ToList().FirstOrDefault();
Console.WriteLine(maxMember.ToString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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