linq 相应地拆分和存储值,并使用特殊字符连接值

2mbi3lxu  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(132)

列=名字、姓氏、聘用日期、中间名等,
传入值= 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;
           
        }
eyh26e7m

eyh26e7m1#

假设每列的列名、值都是不同的所以没有列名和值有值,在替换时会有问题(比如某人有Firstname =“Mr.LastName”)你可以直接用String.Replace

format = "FirstName$LastName_EmailId";
//and s is a list of string of extracted needed column names 
s= {"FirstName", "LastName" ,"EmailId" };

然后像这样得到你想要的值

var record = _smasContext.Users.Where(u => u.UserId == employeeID).FirstOrDefault();

string firstName = record.FirstName ;
string lastName = record.LastName ; 
string emailId = record.EmailId ;
//... so on
string xxx= record.XXX;
string yyy= record.YYY;

然后执行if语句来替换value

string result = format ;
if(  s.Contains("FirstName ")  ) result  = result.Replace("FirstName",firstName   );
if(  s.Contains("LastName ")  ) result  = result.Replace("LastName",lastName );
if(  s.Contains("EmailId ")  ) result  = result.Replace("EmailId",emailId );
//... so on

当然,也有一些高级解决方案,如为词典增值

Dictionary<string, string> dicVal= new Dictionary<string, string>();
dicVal.Add( "FirstName", record.FirstName );  
dicVal.Add( "LastName", record.LastName);  
...
// use column name as key then add all columns  and values 
foreach(string colName in s)
{
  result  = result.Replace(colName  ,dicVal[s]);
}

附加参考How to replace part of string by position?

相关问题