分配给C#中的列表

时间:2017-10-20 17:38:15

标签: c# asp.net-web-api

我正在尝试将值分配给C#中的列表。我有以下课程:

public class Request
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string email { get; set; }
    public List<Service> services { get; set; }

}
public class Service
{
    public string sku { get; set; }
    public string did { get; set;}
}

我在控制器中使用以下代码:

    public async Task<IHttpActionResult> GetCases()
    {
        var client = await 
        SalesforceService.GetUserNamePasswordForceClientAsync();

        //Select All Cases Where Status = New.
        var cases = await client.QueryAsync<Case>("SELECT Id,
        Description FROM Case Where Status='New'");
        var sfCases = cases.Records.Select(c => new
        {
            c.Description
        });

        List<Request> requestresults = new List<Request>();
        foreach (var c in sfCases)
        {

            var sfrequest = new Request();
            if (c.Description != null)
            {
                string[] descriptionline = null;

                sfrequest.Description = c.Description;

                descriptionline = Regex.Split(c.Description, "\r\n");

                if (descriptionline.Any(x => x.Contains("FirstName")))
                {
                    var firstnameindex = Array.FindIndex(descriptionline, row => row.Contains("FirstName"));
                    sfrequest.firstName = descriptionline[firstnameindex].Replace("FirstName - ", "");

                    var lastnameindex = Array.FindIndex(descriptionline, row => row.Contains("LastName"));
                    sfrequest.lastName = descriptionline[lastnameindex].Replace("LastName - ", "");

                    var emailindex = Array.FindIndex(descriptionline, row => row.Contains("Email"));
                    sfrequest.email = descriptionline[emailindex].Replace("Email - ", "");

                    var skuindex = Array.FindIndex(descriptionline, row => row.Contains("sku"));

如何获取skudid并按照与其他属性相同的方式分配它们?

1 个答案:

答案 0 :(得分:-1)

由于您的请求类包含一个名为services的列表,您只需访问以下服务的值:

services.sku
services.did

在您的源代码中,我假设您会像以下一样使用它:

var skuindex = Array.FindIndex(descriptionline, row => row.Contains("sku"));
sfrequest.services.sku = descriptionline[skuindex].Replace("Sku - ", "");

var didindex = Array.FindIndex(descriptionline, row => row.Contains("did"));
sfrequest.services.did = descriptionline[didindex].Replace("Did - ", "");

此外,我必须指出两件事:
- 确保在您尝试访问它时初始化services - 变量,否则会遇到异常。
- 建议使用大写字母作为属性名称的第一个字母。您使用的符号通常用于字段。