Columns = FirstName, LastName, HireDate, MiddleName etc..,
incoming value = Col1$Col2_Col3
These col1, col2, col3 are the column names which will be incoming with special characters added in between. Now this string needs to be split and using linq the value will be replaced from database according to the columns. After the values are received, It needs to be concatenated in the same format with special characters in between and the string will be returned.
Here I have attached a piece of code where I tried to implement the above mentioned problem.
Expected Return value "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;
}