Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Linq更新嵌套列表值?_C#_Linq_List - Fatal编程技术网

C# 使用Linq更新嵌套列表值?

C# 使用Linq更新嵌套列表值?,c#,linq,list,C#,Linq,List,我正在尝试更新C#中的嵌套列表,如下所示 列表 -用户类型 -列表 --UserComponentKey --计数 下面是一个书面示例: 用户名单: 用户类型=1 用户组件 -UserComponentKey=XYZ -计数=3 用户类型=2 用户组件 -UserComponentKey=XYZ -计数=7 我只需要更新UserType 2的UserComponentKey XYZ,目前我的更新已中断,并更新所有用户类型的XYZ。以下是我当前的方法,这些方法不起作用,因为它们更新了包含指定组件键

我正在尝试更新C#中的嵌套列表,如下所示

列表

-用户类型
-列表

--UserComponentKey
--计数

下面是一个书面示例:
用户名单: 用户类型=1
用户组件
-UserComponentKey=XYZ
-计数=3

用户类型=2
用户组件
-UserComponentKey=XYZ
-计数=7

我只需要更新UserType 2的UserComponentKey XYZ,目前我的更新已中断,并更新所有用户类型的XYZ。以下是我当前的方法,这些方法不起作用,因为它们更新了包含指定组件键的所有用户类型的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; }
}
方法2:

if(users.UserType == "2")
{
   foreach(var component in users.UserComponents)
   {
       switch(component.UserComponentKey)
       {
          case "XYZ":
          component.Count = value;
          break;
       }
    }
}
代码生成列表(类似于): List UserComponents=new List()

if(Item.UserAddOn!=null)
{
对于(var i=0;i
我已经去掉了实际值等,但希望有人能给我指出正确的方向


谢谢

您对
first()
的第一次调用是错误的。试着这样做:

Users.Where((us) => us.UserType == "2")
     .Select((us) => us.UserComponents)
     .Where((uc) => uc.UserComponentKey == "XYZ")
     .First()
     .Count = value;
建议:为什么不将
UserType
设置为
int

可能有帮助:

        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);
            }
        }
List_users=newlist();
_添加(新用户(){UserType=“1”,UserComponents=new List(){new UserComponent(){Count=0,UserComponentKey=“XYZ”}});
_添加(新用户(){UserType=“2”,UserComponents=new List(){new UserComponent(){Count=2,UserComponentKey=“XYZ”}});
_添加(新用户(){UserType=“3”,UserComponents=new List(){new UserComponent(){Count=5,UserComponentKey=“XYZ”}});
_users.Where(us=>us.UserType==“2”).First().UserComponents.Where(uc=>uc.UserComponentKey==“XYZ”).First().Count=356;
foreach(用户在_用户中)
{
Console.WriteLine(“UserType:+us.UserType”);
foreach(美国的UserComponent uc.UserComponents)
{
WriteLine(“Key:{0}值:{1}”,uc.UserComponentKey,uc.Count);
}
}

我缺少编写您可以使用的代码片段的信息,因此我将简单地解释一下。对象变量实际上是指向对象所在位置的引用(如果熟悉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

因此,更改值的代码很好,但在建立列表时,如果用户组件未链接在一起,则需要创建不同的用户组件。

您的代码不安全,但应该可以工作。问题是什么?他们当前更新了包含指定组件键的所有用户类型的UserComponent计数值,而不是我要针对的特定用户类型。因此,如果我有4个用户类型,则所有具有组件键“J32S”的用户类型都将使用这些方法进行更新。您能否向我们展示构建此列表的代码?除非出现引用问题,否则所描述的代码不应产生所描述的结果。@Pluc Done,您能提出任何建议吗?因为usertype实际上是一个字符串,请立即尝试。如果这不是问题的原因,则所有的
usertype
都具有相同的值(2)。这不一定是真的。如果向每个用户添加了相同的UserComponent(XYZ)实例,那么在一个用户中修改它就像在每个用户中修改它一样。这就是为什么我要求代码生成他的列表。用户类型实际上是:“基本”、“高级”,所以它们不一样。UC在当前上下文中不存在?我们已经解决了它。就像你在列表生成中说的那样。我已经把列表生成分离到它自己的函数中,它被调用来立即创建空白组件。谢谢
        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);
            }
        }
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