将值映射到列表中的项目并将新值添加到同一列表C#

时间:2016-02-03 06:20:32

标签: c# list dictionary

我有一系列颜色即。

var colorPallete = new string[]{color1, color2, color3, color4, color5};

我还有一个包含ID的对象列表。 例如。 var previousList<MyModel> = new List<MyModel>();

MyModel.cs

public class MyModel()
{
   public int ID {get; set;}
public string Class{get; set;}
public string Name {get; set;}
public string Color {get; set;}
}

我想为具有相同ID的对象分配特定颜色。然后将指定的颜色添加为列表中的新值。

例如:

上一个清单: -

            ID :1
            Name: abc
            Class: Senior

           ID :2
            Name: xyz
            Class: Medium

            ID :3
            Name: pqr
            Class: junior

           ID :1
            Name: mno
            Class: junior

新名单: -

            ID :1
            Name: abc
            Class: Senior
            Color :color1

            ID :2
            Name: xyz
            Class: Medium
            Color :color2

            ID :3
            Name: pqr
            Class: junior
            Color :color3

            ID :1
            Name: mno
            Class: junior
            Color :color1

3 个答案:

答案 0 :(得分:3)

这对我有用:

var colorPallete = new string[]
{
    "color1", "color2", "color3", "color4", "color5",
};

var previousList = new []
{
    new { ID = 1, Name = "abc", Class = "Senior", },
    new { ID = 2, Name = "xyz", Class = "Medium", },
    new { ID = 3, Name = "pqr", Class = "junior", },
    new { ID = 1, Name = "mno", Class = "junior", },
};

var newList =
    previousList
        .Select(x => new
        {
            x.ID,
            x.Name,
            x.Class,
            Color = colorPallete.ElementAtOrDefault(x.ID - 1),
        })
        .ToList();

我得到了这个结果:

result

随着问题更新提供类MyModel,代码可以这样编写:

var colorPallete = new string[]
{
    "color1", "color2", "color3", "color4", "color5",
};

var previousList = new List<MyModel>()
{
    new MyModel() { ID = 1, Name = "abc", Class = "Senior", },
    new MyModel() { ID = 2, Name = "xyz", Class = "Medium", },
    new MyModel() { ID = 3, Name = "pqr", Class = "junior", },
    new MyModel() { ID = 1, Name = "mno", Class = "junior", },
};

var newList =
    previousList
        .Select(x => new MyModel()
        {
            ID = x.ID,
            Name = x.Name,
            Class = x.Class,
            Color = colorPallete.ElementAtOrDefault(x.ID - 1),
        })
        .ToList();

给出了:

MyModel result

现在,这种方法会生成一个新列表,保持旧列表和旧对象的完整性。通常这是你应该尝试做的。只有当你知道它们的设计目的时才最好改变对象。

因此可以像这样对原始列表进行就地更新:

previousList.ForEach(x => x.Color = colorPallete.ElementAtOrDefault(x.ID - 1));

这导致修改previousList个对象而不创建newList

答案 1 :(得分:0)

我会为具有color属性的对象创建一个类:

public class MyClass
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Class { get; set; }
    public string Color { get; set; } // Nullable
}

对于颜色,我会创建另一个带有ID的类,以与MyClass的ID进行比较:

public class MyColor
{
    public int ID { get; set; }
    public string Color { get; set; }
}

对于colorPalette中的每种颜色,您将指定一个与MyClass列表的ID相匹配的ID。

首先,MyClass的颜色为空。然后你可以循环遍历MyClass

列表
foreach (MyClass myClass in myClassList)
{
    myClass.Color = colorPalette.FirstOrDefault(col => col.ID = myClass.ID);
}

或者在Color类中没有ID(比较变量名称不是一个漂亮的解决方案):

foreach (MyClass myClass in myClassList)
{
    myClass.Color = colorPalette.FirstOrDefault(col => int.Parse(nameof(col.Color).Replace("color", "")) == myClass.ID);
}

答案 2 :(得分:0)

如果您使用的是List<T>(不是IEnumerable<T>)并且您不想创建新列表,但需要更新现有列表中的值,则可以使用单个查询。处理场景有三种方法(A,B,C):

var colorPallete = new string[]
{
    "Red", "Green", "Blue"
};

var list = new List<MyModel>()
{
    new MyModel() { ID = 1, Name = "model1", Class = "A", },
        new MyModel() { ID = 1, Name = "model11", Class = "AA", },
        new MyModel() { ID = 2, Name = "model2", Class = "B", },
        new MyModel() { ID = 3, Name = "model3", Class = "C", },                            
        new MyModel() { ID = 4, Name = "model4", Class = "D", },
        new MyModel() { ID = 5, Name = "model5", Class = "E", },            
};

//A. This code assigns null for unknown IDs
//I.e. if (ID > 0 && ID < colorPallete.Length) then color will be picked from colorPallete[],
//else it will be null
list.ForEach(x => x.Color = colorPallete.ElementAtOrDefault(x.ID - 1));

//B. This code apply some default color for unknown IDs
//I.e. if (ID > 0 && ID < colorPallete.Length) then color will be picked from colorPallete,
//else it will be "DefaultColor"
list.ForEach(x => x.Color = colorPallete.ElementAtOrDefault(x.ID - 1) ?? "DefaultColor");

//C. This code can assign the same color to models with different IDs, 
//but models with identical IDs always will have identical color
list.ForEach(x => x.Color = colorPallete.ElementAtOrDefault((x.ID - 1) % colorPallete.Length));