-4

我有一个 List<> 像这样填充:

ID mukey FieldID 类型 PercentOfField 英亩
-------------------------------------------------- -------------
1 1709649 191 未成年人 18 12.5181
2 1709641 191 未成年人 1 0.49621
3 1709620 191 严重、严重 72 49.4322
4 1709622 191 未成年人 9 5.89865

我想将第 3 项的主要类型为关键,并将其拆分为具有完全相同数据的两条记录,但其中一条记录的类型为主要,另一条记录的类型为关键。如果我使用 foreach 遍历列表,我可以将此记录分成两部分吗?

4

3 回答 3

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();
于 2012-07-12T15:05:29.920 回答
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));
        }
    }
}
于 2012-07-12T15:09:08.550 回答
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++;
        }
    }
}
于 2012-07-12T15:23:28.057 回答