列=名字、姓氏、聘用日期、中间名等,
传入值= Col1$Col2_Col3
这些col 1,col 2,col 3是列名称,它们之间将添加特殊字符。现在需要拆分此字符串,并使用linq将根据列从数据库中替换值。在接收到值后,需要以相同的格式将其连接起来,并在中间添加特殊字符,然后返回字符串。
这里我已经附上了一段代码,我试图实现上述问题。
预期返回值“Rob$Steve_Liam”
public string AutoCredential(string credential, int employeeID)
{
Char[] specialChar = { '_', '$', '&' };
string? format = _itzmeinContext.AppSettings.Where(a => a.AppKey == credential).Select(a => a.AppValue).FirstOrDefault();
var userTypes = _itzmeinContext.GlobalLookupData.Where(ut => ut.GlobalLookupTableNameId == 7).
Select(ut => ut.LookupName).ToList();
var unFormat = format.Split(specialChar);
var s = unFormat.Intersect(userTypes);
string FirstName = null!;
string MiddleName = null!;
string LastName = null!;
string EmailId = null!;
string PrimaryContactNumber = null!;
string SecondaryContactNumber = null!;
string HireDate = null!;
if (s.Contains("FirstName"))
{
FirstName = _smasContext.Users.Where(u => u.UserId == employeeID).Select(u => u.FirstName).FirstOrDefault();
}
if (s.Contains("MiddleName"))
{
MiddleName = _smasContext.Users.Where(u => u.UserId == employeeID).Select(u => u.MiddleName).FirstOrDefault();
}
if (s.Contains("LastName"))
{
LastName = _smasContext.Users.Where(u => u.UserId == employeeID).Select(u => u.LastName).FirstOrDefault();
}
if (s.Contains("EmailId"))
{
EmailId = _smasContext.Users.Where(u => u.UserId == employeeID).Select(u => u.EmailId).FirstOrDefault();
}
if (s.Contains("PrimaryContactNumber"))
{
PrimaryContactNumber = _smasContext.Users.Where(u => u.UserId == employeeID).Select(u => u.PrimaryContactNumber).FirstOrDefault();
}
if (s.Contains("SecondaryContactNumber"))
{
SecondaryContactNumber = _smasContext.Users.Where(u => u.UserId == employeeID).Select(u => u.SecondaryContactNumber).FirstOrDefault();
}
if (s.Contains("HireDate"))
{
HireDate = _smasContext.Users.Where(u => u.UserId == employeeID).Select(u => u.HireDate.Date.Year.ToString()).FirstOrDefault();
}
string result = string.Concat(FirstName + MiddleName + LastName + EmailId + PrimaryContactNumber + SecondaryContactNumber + HireDate);
return result;
}
1条答案
按热度按时间eyh26e7m1#
假设每列的列名、值都是不同的所以没有列名和值有值,在替换时会有问题(比如某人有Firstname =“Mr.LastName”)你可以直接用String.Replace
然后像这样得到你想要的值
然后执行if语句来替换value
当然,也有一些高级解决方案,如为词典增值
附加参考How to replace part of string by position?