基于没有组的值拆分列表

时间:2018-02-22 10:20:21

标签: c# list split

我想按值拆分列表,但我不需要对结果进行分组, 这是我的清单:

var source = new List<Car>() {
    new Car() { ID = 1, Model = "Model A" },
    new Car() { ID = 2, Model = "Model B" },
    new Car() { ID = 3, Model = "Model B" },
    new Car() { ID = 4, Model = "Model B" },
    new Car() { ID = 5, Model = "Model A" },
    new Car() { ID = 6, Model = "Model A" }
};
var result = GetListOfAdjacentCarsOfSameModel();

我希望结果列表看起来像这样:

- Model A
    - ID = 1, Model = "Model A"
- Model B
    - ID = 2, Model = "Model B"
    - ID = 3, Model = "Model B"
    - ID = 4, Model = "Model B" 
- Model A   
    - ID = 5, Model = "Model A"
    - ID = 6, Model = "Model A"

3 个答案:

答案 0 :(得分:1)

我建议使用简单循环

var result = new List<List<Car>>();

foreach (var item in source)
  if (result.Count <= 0 || result[result.Count - 1][0].Model != item.Model)
    result.Add(new List<Car>() {item}); // Create a new group, add item to it
  else
    result[result.Count - 1].Add(item); // Add item into the last group

答案 1 :(得分:0)

public class Car
{
    public int ID { get; set; }
    public string Model { get; set; }
}

public class Result
{
    public string Model { get; set; }
    public int Count { get; set; }
}


var source = new List<Car>();
source.Add(new Car { ID = 1, Model = "Model A" });
source.Add(new Car { ID = 1, Model = "Model B" });
source.Add(new Car { ID = 1, Model = "Model B" });
source.Add(new Car { ID = 1, Model = "Model B" });
source.Add(new Car { ID = 1, Model = "Model A" });
source.Add(new Car { ID = 1, Model = "Model A" });

var result = new List<Result>();
if (source.Any())
{
    string model = source[0].Model;
    int count = 0;
    foreach (var car in source)
    {
        if (car.Model == model)
        {
            count++;
        }
        else
        {
            result.Add(new Result { Model = model, Count = count });
            model = car.Model;
            count = 1;
        }
    }
    result.Add(new Result { Model = model, Count = count });
}

foreach (var r in result)
{
    Console.WriteLine(r.Model + " - " + r.Count);
}

答案 2 :(得分:0)

我正在努力寻找一种简单的方法来做到这一点,但你总是可以迭代地做到这一点:

var source = new List<string>() {
    "Model A",
    "Model B",
    "Model B",
    "Model B",
    "Model A",
    "Model A"
};

string lastModel = null;
List<string> currentList = new List<string>();
var result = new List<List<string>>();

foreach (var model in source)
{
    if (lastModel != null && model != lastModel)
    {
        result.Add(currentList);
        currentList = new List<string>();
    }
    currentList.Add(model);
    lastModel = model;
}

if (currentList.Any()) {
    result.Add(currentList);
}

现在,可以很容易地将该代码包装在您可以在汽车类中使用的扩展方法中:

var source = new List<Car>() {
    new Car { Model = "Model A" },
    new Car { Model = "Model B" },
    new Car { Model = "Model B" },
    new Car { Model = "Model B" },
    new Car { Model = "Model A" },
    new Car { Model = "Model A" }
};

var result = source.CollectAdjacentWithSameModel();

// elsewhere    
public static class EnumerableExtensions
{
    public static IEnumerable<IEnumerable<Car>> CollectAdjacentWithSameModel(this IEnumerable<Car> source)
    {
        var lastModel = default(Car);
        List<Car> currentList = new List<Car>();
        var result = new List<List<Car>>();

        foreach (var model in source)
        {
            if (lastModel != null && model.Model != lastModel.Model)
            {
                result.Add(currentList);
                currentList = new List<Car>();
            }
            currentList.Add(model);
            lastModel = model;
        }

        if (currentList.Any())
        {
            result.Add(currentList);
        }

        return result;
    }
}

public class Car {
    public string Model { get; set; }
}