Trim ASP.NET MVC Api 调用 C# 中每个逗号后除 2 个字符外的所有字符

Trim all but 2 characters after comma in for each in ASP.NET MVC Api call C#

在我的 for each 循环中,我需要 trim 在我的存储过程中返回全名。我需要“姓氏,名字的前 2 个字符”,但 trim 其他所有内容。

foreach (var mrnRecord in mrnRecords)
{
    var filtered = dt.AsEnumerable().Where(x => x.Field<string>("MRN").Contains(mrnRecord));

    patient = new Patient();
    orders = new List<Order>();

    patient.MRN = filtered.First().Field<string>("MRN");
    patient.CurrentLocation = filtered.First().Field<string>("CurrentLocation") + "<br />" + filtered.First().Field<string>("PatientName") + "<br />" + filtered.First().Field<string>("Isolation");
    patient.PatientName = filtered.First().Field<string>("PatientName");
    patient.PatientType = filtered.First().Field<string>("PatientType");
    patient.VisitId = filtered.First().Field<string>("VisitID");

假设名字是“First Middle Last”,您可以使用

var myString = "Franklin Delano Roosevelt";
var stringArr = myString.Split(' ');
var lastFirstTwoInitials = stringArr[2] + ", " + stringArr[0].Substring(0, 2);

编辑:刚刚看到您关于格式为“最后,第一个中间”的评论

var stringArr = patient.PatientName.Split(' ');
var lastFirstTwoInitials = stringArr[0] +  " " + stringArr[1].Substring(0, 2);

var stringArr = patient.PatientName.Split(' ');
var lastFirstTwoInitials = patient.PatientName.Substring(0, stringArr[0].Length + 3);