这是我的情景!
List<String> list = new List<String>();
list.Add("E9215001");
list.Add("E9215045");
list.Add("E1115001");
list.Add("E1115022");
list.Add("E1115003");
list.Add("E2115041");
list.Add("E2115042");
list.Add("E4115021");
list.Add("E5115062");
Run Code Online (Sandbox Code Playgroud)
我需要使用C#和LINQ从上面的列表中提取以下常用部分
E92150 - >摘自{*E92150*01,*E92150*45}
E11150 - >摘自{*E11150*01,*E11150*22,*E11150*03}
E21150 - >摘自{*E21150*41,*E21150*42}
E41150 - >摘自{*E41150*21}
E51150 - >摘自{*E51150*62}
list.Select((item, index) => new {
Index=index,
Length=Enumerable.Range(1, (item.Length-2)) //I'm ignoring the last 2 characters
.Reverse()
.First(proposedLength => list.Count(innerItem =>
innerItem.StartsWith(item.Substring(0, proposedLength))) >
1)}).Select(n => list[n.Index].Substring(0, n.Length)).Distinct()
Run Code Online (Sandbox Code Playgroud)
但是,我怀疑这是你正在寻找的东西
var result = list.Select(s => s.Substring(0, 6))
.Distinct();
Run Code Online (Sandbox Code Playgroud)