我颳起了這個代碼在一個控制檯應用程序相當快的應該做的伎倆....假設你是在處理一個強類型有一個定義類似如下:
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++;
}
}
}
是場在每個製表符分隔的製表符? – 2012-07-12 15:02:32
查看C#split/substring方法。這些可能是你正在尋找的。 – Purplegoldfish 2012-07-12 15:02:45
我把表格格式化了,但刪除了一些細節,希望沒關係。請發佈相關代碼。你如何在你的列表中存儲記錄? – oleksii 2012-07-12 15:06:47