2012-07-12 77 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)); 
     } 
    } 
}