如何在字典类型会话变量中添加逗号分隔的字符串值

Man*_*ngh 1 c# dictionary

我正在使用c#

我的变量中有以下字符串.

string results = "Mr,Mike,Lewis,32,Project Manager,India";
Run Code Online (Sandbox Code Playgroud)

现在我想在会话变量的Dictionary类型中添加这些值.我在代码中声明了一个dict类型变量.

Dictionary<string, string> skywardsDetails = new Dictionary<string, string>();
Run Code Online (Sandbox Code Playgroud)

现在写下我编写的代码如下:

if (!string.IsNullOrEmpty(results))                                            
{                                                
    string[] array = results.Split(',');
    string title = array[0];
    string firstname = array[1];
    string lastname = array[2];
    string age = array[3];
    string designation = array[4];    
    string country = array[4];    

    //Here I want to write the new code which will add the results.Split(',') values in my Session variable as a Dictionary type.                                       

    foreach (string key in results.Split(','))
    {
    skywardsDetails.Add(key,//What to do here)
    }
}
Run Code Online (Sandbox Code Playgroud)

请建议

Dar*_*rov 5

您的CSV results变量不代表字典.它代表了一个Employee模型:

public class Employee
{
    public string Title { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public string Designation { get; set; }
    public string Country { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后:

var tokens = (results ?? string.Empty).Split(',');
if (tokens.Length > 5)
{
    var employee = new Employee
    {
        Title = tokens[0],
        FirstName = tokens[1],
        LastName = tokens[2],
        Age = int.Parse(tokens[3]),
        Designation = tokens[4],
        Country = tokens[5]
    };
    // TODO: do something with this employee like adding it
    // to some dictionary, session, whatever
}
Run Code Online (Sandbox Code Playgroud)