2012-07-12 80 views
-4

我有一個List <>填充像這樣:如何在C#中將List的項目拆分爲兩個?

 
ID mukey  FieldID  Type  PercentOfField Acres 
--------------------------------------------------------------- 
1 1709649  191  Minor    18  12.5181 
2 1709641  191  Minor    1  0.49621 
3 1709620  191  Major, Critical 72  49.4322 
4 1709622  191  Minor    9  5.89865 

我想借3項與主要,關鍵的類型,並將其與除非該類型將主要完全相同的數據分成兩個記錄對於一條記錄而言並且對於另一條記錄是嚴重的如果我使用foreach循環訪問列表,我可以將此記錄分成兩部分嗎?

+0

是場在每個製表符分隔的製表符? – 2012-07-12 15:02:32

+0

查看C#split/substring方法。這些可能是你正在尋找的。 – Purplegoldfish 2012-07-12 15:02:45

+0

我把表格格式化了,但刪除了一些細節,希望沒關係。請發佈相關代碼。你如何在你的列表中存儲記錄? – oleksii 2012-07-12 15:06:47

回答

0

我颳起了這個代碼在一個控制檯應用程序相當快的應該做的伎倆....假設你是在處理一個強類型有一個定義類似如下:

public class MyObject 
{ 
    public int ID { get; set; } 
    public string Mukey { get; set; } 
    public int FieldID { get; set; } 
    public string Type { get; set; } 
    public int PercentOfField { get; set; } 
    public double Acres { get; set; } 
} 

使用下面將做您要求什麼(如果我理解正確的問題)

var myList = new List<MyObject>() { 
    new MyObject { ID = 1, Mukey = "1709649", FieldID = 191, Type = "Minor", PercentOfField = 18, Acres = 12.5181 }, 
    new MyObject { ID = 2, Mukey = "1709641", FieldID = 191, Type = "Minor", PercentOfField = 1, Acres = 0.49621 }, 
    new MyObject { ID = 3, Mukey = "1709620", FieldID = 191, Type = "Minor, Critical", PercentOfField = 72, Acres = 49.4322 }, 
    new MyObject { ID = 4, Mukey = "1709622", FieldID = 191, Type = "Minor", PercentOfField = 9, Acres = 5.89865 } 
}; 

for (int i = 0; i < myList.Count; i++) 
{ 
    //check if the type is comma delimited 
    if (myList[i].Type.Contains(",")) 
    { 
     //get the separate types 
     string[] types = myList[i].Type.Split(','); 

     var item = myList[i]; 
     myList.RemoveAt(i); 

     //for each type, add a new entry in the list 
     foreach (string theType in types) 
     { 
      myList.Insert(
       i, 
       new MyObject 
       { 
        ID = item.ID, 
        Mukey = item.Mukey, 
        FieldID = item.FieldID, 
        Type = theType.Trim(), 
        PercentOfField = item.PercentOfField, 
        Acres = item.Acres 
       } 
      ); 
      // add to i to offset the count for the additional items 
      // (we want to skip the new items) 
      i++; 
     } 
    } 
} 
+0

這正是我一直在尋找的!謝謝。 – user1489808 2012-07-12 16:45:33

+0

一個漂亮的糖編碼答案。 – Amicable 2012-07-12 18:29:05

2

你的意思是這樣(未經測試,把我的頭頂部):

var splitList = myList.SelectMany(x => x.Type.Split(", ").Select(t => new myClass(x.ID, x.mukey, x.FieldID, t, x.PercentOfField, x.Acres)).ToList(); 

當然,這使得在內存中的每行的新副本,所以它可能不長了最佳解決方案表...也許這是更好的(再次未經測試,但你的想法):

var splitList = myList.SelectMany(x => x.Type.Contains(", ") ? x.Type.Split(", ").Select(t => new myClass(x.ID, x.mukey, x.FieldID, t, x.PercentOfField, x.Acres) : new myClass[] {x}).ToList(); 
0

你只需要找到一個在該列中多個值的任何項目,並刪除它們,並多次重新添加,每個價值一次。舉例來說,假設你的列表是製表符分隔字符串列表,這樣的事情應該工作:

List<string> records = "..."; 
foreach (string record in records.ToArray()) 
{ 
    string[] fields = record.Split('\t'); 
    string[] types = fields[3].Split(','); 
    if (types.Length > 1) 
    { 
     records.Remove(record); 
     foreach (string type in types) 
     { 
      fields[3] = type.Trim(); 
      records.Add(string.Join('\t', fields)); 
     } 
    } 
}