2017-10-20 57 views
-2

我試圖給C#中的列表賦值。我有以下類:分配給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併爲它們分配的方式,我做了其他性質相同?

+4

什麼和你的問題到底在哪裏?在代碼中突出顯示它.. – Coding

+2

歡迎來到Stack Overflow!當提出問題時,請嘗試製作[最小完整且可驗證](https://stackoverflow.com/help/mcve)您問題的示例。現在你有很多代碼可能與這個問題沒有多大關係。正如@Coding所述,目前還不清楚問題的癥結所在。這並不容易,但它是使用軟件的一部分。 –

回答

-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 - 變量在您嘗試訪問它的位置進行初始化,否則會遇到異常。
- 建議使用大寫字母來表示屬性名稱的第一個字母。您使用的符號通常用於字段。