我有一个字符串列表,我想通过在它的末尾添加一个数字来使该列表中的每个字符串都是唯一的.此外,它不区分大小写,因此"apple"应该被认为是"Apple"或"apPlE"
例如:
List<string> input = new List<string>();
input.Add("apple");
input.Add("ball");
input.Add("apple");
input.Add("Apple");
input.Add("car");
input.Add("ball");
input.Add("BALL");
Run Code Online (Sandbox Code Playgroud)
预期产量:
"apple","ball","apple2","Apple3","car","ball2","BALL3"
我需要帮助来开发产生输出的逻辑.谢谢.
编辑:我不能有0和1,重复的字符串必须以2,3,4开头...
var newList = input.GroupBy(x => x.ToUpper())
.SelectMany(g => g.Select((s, i) => i == 0 ? s : s + (i+1)))
.ToList();
var str = String.Join(", ", newList);
Run Code Online (Sandbox Code Playgroud)
编辑
var newList = input.Select((s, i) => new { str = s, orginx = i })
.GroupBy(x => x.str.ToUpper())
.Select(g => g.Select((s, j) => new { s = j == 0 ? s.str : s.str + (j + 1), s.orginx }))
.SelectMany(x => x)
.OrderBy(x => x.orginx)
.Select(x => x.s)
.ToList();
Run Code Online (Sandbox Code Playgroud)