使用Linq更新嵌套列表值?

时间:2015-04-23 12:20:42

标签: c# linq list

我正在尝试更新C#中的嵌套列表,看起来像这样

列表<Users&GT;
- 用户类型
- 列出<UserComponents&gt;
- - UserComponentKey
- - 计数

这是一个书面的例子:
用户列表: UserType = 1
UserComponents
- UserComponentKey = XYZ
- 计数= 3

UserType = 2
UserComponents
- UserComponentKey = XYZ
- 计数= 7

我只需要为UserType 2更新UserComponentKey XYZ,目前我的更新已被破坏,并为所有用户类型更新XYZ。这是我当前的方法不起作用,因为它们更新包含指定组件键的ALL usertypes的UserComponent计数值,而不是我目标的特定用户类型。

类:

public class Users
{
    public string UserType { get; set; }
    public List<UserComponent> UserComponents { get; set; }
}

public class UserComponent
{
    public string UserComponentKey { get; set; }
    public int Count { get; set; }
}

方法1:

Users.Where(us => us.UserType == "2")
     .First().UserComponents
     .Where(uc => uc.UserComponentKey == "XYZ")
     .First().Count = value;

方法2:

if(users.UserType == "2")
{
   foreach(var component in users.UserComponents)
   {
       switch(component.UserComponentKey)
       {
          case "XYZ":
          component.Count = value;
          break;
       }
    }
}

代码生成列表(类似于):     列出UserComponents = new List();

if (Item.UserAddOn != null)
    {
        for (var i = 0; i < Item.UserAddOn.First().Count; i++)
        {
            UserComponents.Add(new UserComponent
            {
                UserComponentKey = Item.UserAddOn[i].ComponentKey,
                Count = 0
             });
         }
    }

if (Item.User != null)
    {
        for (var i = 0; i < Item.User.First().Count; i++)
        {
             Users.Add(new User()
             {
                 UserType = Item.User[i].ComponentKey,
                 Count = 0,
                 UsersComponents = UserComponents
             });
         }
     }

我已经删除了实际值等,但希望有人可以指出我在这里正确的方向。

谢谢!

3 个答案:

答案 0 :(得分:0)

您第一次拨打First()是错误的。试试这样:

Users.Where((us) => us.UserType == "2")
     .Select((us) => us.UserComponents)
     .Where((uc) => uc.UserComponentKey == "XYZ")
     .First()
     .Count = value;

建议:为什么不让UserType成为int

答案 1 :(得分:0)

可能有帮助:

        List<Users> _users = new List<Users>();
        _users.Add(new Users() { UserType = "1", UserComponents = new List<UserComponent>() { new UserComponent() { Count = 0, UserComponentKey = "XYZ" } } });
        _users.Add(new Users() { UserType = "2", UserComponents = new List<UserComponent>() { new UserComponent() { Count = 2, UserComponentKey = "XYZ" } } });
        _users.Add(new Users() { UserType = "3", UserComponents = new List<UserComponent>() { new UserComponent() { Count = 5, UserComponentKey = "XYZ" } } });

        _users.Where(us => us.UserType == "2").First().UserComponents.Where(uc => uc.UserComponentKey == "XYZ").First().Count = 356;

        foreach (Users us in _users)
        {
            Console.WriteLine("UserType: " + us.UserType);
            foreach (UserComponent uc in us.UserComponents)
            {
                Console.WriteLine("Key: {0} Value: {1}", uc.UserComponentKey, uc.Count);
            }
        }

答案 2 :(得分:0)

我缺少编写您可以使用的代码段的信息,因此我将简单解释一下。对象变量实际上是对象所在位置的引用(如果您熟悉C ++ / C,则为指针)。将对象添加到列表时,可以添加它的位置。如果将此对象添加到多个列表,则会给出相同的位置,因此编辑其中一个将编辑所有这些。

var uc1 = new UserComponent { Count = 1 };
var uc2 = new UserComponent { Count = 2 };
var uc3 = new UserComponent { Count = 2 };

var u1 = new User();
var u2 = new User();

u1.UserComponents.Add(uc1);
u1.UserComponents.Add(uc2);

u2.UserComponents.Add(uc1);
u2.UserComponents.Add(uc3);

Console.Write(u1.UserComponents[0].Count); //Outputs 1
Console.Write(u1.UserComponents[1].Count); //Outputs 2
Console.Write(u2.UserComponents[0].Count); //Outputs 1
Console.Write(u2.UserComponents[1].Count); //Outputs 2

u2.UserComponents[0].Count = 5;
u2.UserComponents[1].Count = 6;

Console.Write(u1.UserComponents[0].Count); //Outputs 5
Console.Write(u1.UserComponents[1].Count); //Outputs 6
Console.Write(u2.UserComponents[0].Count); //Outputs 5
Console.Write(u2.UserComponents[1].Count); //Outputs 2

因此,更改值的代码很好,但是当您构建列表时,如果它们没有链接在一起,则需要创建不同的UserComponents。