2015-04-23 108 views
0

我试图更新在C#中,看起来像这样使用Linq更新嵌套列表值?

名单<Users>
嵌套列表 - 用户类型
- 列表<UserComponents>
- - UserComponentKey
- - 计数

这里有一个书面例如:
用户列表: UserType = 1
UserComponents
- UserComponentKey = XYZ
- 计数= 3

用户类型= 2个
UserComponents
- UserComponentKey = XYZ
- 计数= 7

我需要更新UserComponentKey XYZ的用户类型2只,目前我更新已中断,并更新所有用户类型的XYZ。这是我当前的方法,它不工作,因为它们更新包含指定组件密钥的所有usertype的UserComponent计数值,而不是我所针对的特定usertype。

CLASSES:

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; 
     } 
    } 
} 

码生成LIST(类似): 列表UserComponents =新列表();

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 
      }); 
     } 
    } 

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

谢谢!

+0

你的代码是不安全的,但它应该工作。问题是什么? – Pluc

+0

它们当前更新包含指定组件密钥的所有usertype的UserComponent计数值,而不是我指定的特定usertype。 – Dan

+0

所以,如果我有4个usertypes,所有与组件键“J32S”所有四个将使用这些方法更新。 – Dan

回答

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。

+0

我们已经解决了。就像你建议它在列表生成一样。我已经把列表生成分离出来了,它被调用来自动创建空白组件。谢谢。 – Dan

0

您的第一个电话First()是错误的。试着这样说:

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

建议:你为什么不做出UserTypeint

+1

因为usertype实际上是一个字符串,现在就试试这个。 – Dan

+0

如果这不是导致您的问题的原因,那么您所有的'UserType'都具有相同的值(2)。 –

+0

这不一定是真的。如果将UserComponent(XYZ)的相同实例添加到每个用户,则修改它在一个用户中似乎就像您在每个用户中更改它一样。这就是为什么我要求生成他的列表的代码。 – Pluc

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); 
      } 
     }