Home > Enterprise >  To split and store the value accordingly and concatenate the values with special character
To split and store the value accordingly and concatenate the values with special character

Time:12-02

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;
           
        }

CodePudding user response:

Assume that column names,value of each columns are difference so there is no column name and value has the value that will have problems when replace(e.g. like someone has Firstname = "Mr.LastName") you can just use String.Replace

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

then you go the value you want like this

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;

then do the if statements to replace 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

of course there are some advance solution like add value to dictionary

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]);
}

Additional reference How to replace part of string by position?

  • Related