我尝试了过去1周的以下场景,我无法获得以下名称组合方案的逻辑,我已经尝试过C#Linq查询但是它没有给出预期的结果所以我来这里寻求帮助
下面是我试过的代码
public List<string> NameCombination(string FnameLname, string OtherNames)
{
List<string> _FirstTable = new List<string>();
List<string> _SecondTable= new List<string>();
List<string> ResultNameCombination= new List<string>();
string [] _FirstTableArr= FnameLname.Split(' ');
string [] _SecondTableArr = OtherNames.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
_FirstTable.Add(_FirstTableArr[0]);
_FirstTable.AddRange(_SecondTableArr);
_SecondTable.Add(_FirstTableArr[1]);
_SecondTable.AddRange(_SecondTableArr);
var result1 = from a in _FirstTable
from b in _SecondTable
where a != b
select a +" "+ b;
return ResultNameCombination=result1.ToList();
}
我有一个场景,我将拥有名字: sue 姓氏:史密斯以及另外一个名为其他名称的列:可以是单个字或两个字或多个用空格分隔的单词
现在我需要根据以下规则为名字,姓氏和其他名称制作如下名称组合
场景1:其他名称是单个字
First name: Sue
Last Name: Smith
Other Name: Susan
名称组合结果:
Sue Smith
Susan Smith
Sue Susan
情景2:其他名称中的两个单词
First name: Sue
Last Name: Smith
Other Name: Susan Wilson
名称组合结果:
Sue Smith
Susan Wilson
Sue Wilson
Susan Smith
场景3:其他名称中的三个单词
First name: Sue
Last Name: Smith
Other Name: Susan con Wilson
名称组合结果:
Sue Smith
Susan Wilson
Sue Wilson
Susan Smith
场景4:其他名称中的四个单词
名字:苏 姓氏:史密斯 其他名称:Susan con kelly Wilson
名称组合结果:
Sue Smith
Susan kelly
Susan Wilson
Sue Kelly
Sue Wilson
Susan Smith
场景5:其他名称中的五个单词
First name: Sue
Last Name: Smith
Other Name: Susan con kelly wil Wilson
名称组合结果:
Sue Smith
Susan kelly
Susan Wilson
Susan Wil
Sue Kelly
Sue Wilson
Sue wil
Susan Smith
我需要在C#
中处理上述逻辑答案 0 :(得分:1)
使用扩展方法将IEnumerable<T>
与单个T
public static IEnumerable<T> StartWith<T>(this IEnumerable<T> rest, params T[] first) => first.Concat(rest);
我认为这些名称并不相同,以便更容易处理单一名称案例:
var OtherName = "Susan Wilson";
var firstName = "Sue";
var lastName = "Smith";
var otherNames = Regex.Split(OtherName, @"\s+");
var possibleFirstNames = otherNames.Take(1).StartWith(firstName);
var possibleLastNames = otherNames.Skip(otherNames.Length > 3 ? 2 : otherNames.Length-1).StartWith(lastName);
var possibleNames = (from fn in possibleFirstNames
from ln in possibleLastNames
where fn != ln
select fn+" "+ln).Distinct();