Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# 在C中的列表中插入一个对象#_C#_Linq - Fatal编程技术网

C# 在C中的列表中插入一个对象#

C# 在C中的列表中插入一个对象#,c#,linq,C#,Linq,我有一个类对象列表UserData。我通过where方法从该列表中获取一个对象 UserData.Where(s => s.ID == IDKey).ToList(); //ID is unique 我想对对象进行一些更改,并在列表中的同一位置插入。但是,我没有这个对象的索引 知道怎么做吗 谢谢您可以使用该方法获取索引 UserData.FindIndex(s=>s.ID==IDKey) 它将返回一个int。当您从列表中获取项目时,它是一个引用类型,如果您对其更新任何内容,它将自动更改列

我有一个类对象列表
UserData
。我通过
where
方法从该列表中获取一个对象

UserData.Where(s => s.ID == IDKey).ToList(); //ID is unique
我想对对象进行一些更改,并在列表中的同一位置插入。但是,我没有这个对象的索引

知道怎么做吗


谢谢

您可以使用该方法获取索引
UserData.FindIndex(s=>s.ID==IDKey)

它将返回一个int。

当您从列表中获取项目时,它是一个引用类型,如果您对其更新任何内容,它将自动更改列表中的值。更新后请检查您自己

无论你从哪里得到的东西

UserData.Where(s => s.ID == IDKey).ToList(); 

是引用类型。

只需使用
SingleOrDefault
获取对象并进行相关更改;您无需再次将其添加到列表中;您只需更改同一个实例,它是列表中的一个元素

var temp = UserData.SingleOrDefault(s => s.ID == IDKey);
// apply changes
temp.X = someValue;

只要
UserData
是引用类型,列表就只包含对该对象实例的引用。因此,您无需删除/插入即可更改其属性(显然不需要该对象的索引)

我还建议您使用
Single
方法(而不是
ToList()
),只要id是唯一的

示例

public void ChangeUserName(List<UserData> users, int userId, string newName)
{
     var user = users.Single(x=> x.UserId == userId);
     user.Name = newName;  // here you are changing the Name value of UserData objects, which is still part of the list
}
public void ChangeUserName(列出用户、int userId、字符串newName)
{
var user=users.Single(x=>x.UserId==UserId);
user.Name=newName;//这里您正在更改UserData对象的名称值,它仍然是列表的一部分
}

如果我误解了你,请纠正我,但我想你是说你本质上想迭代列表中的元素,如果它匹配一个条件,那么你想以某种方式修改它并将其添加到另一个列表中

如果是这种情况,请参阅下面的代码,了解如何使用Where子句编写匿名方法。Where子句只需要与以下内容匹配的匿名函数或委托:

参数:ElementType元素,int索引--返回:bool结果

允许它根据布尔返回选择或忽略元素。这使我们能够提交一个简单的布尔表达式,或一个更复杂的函数,该函数具有以下附加步骤:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StackOverflow
{
    class Program
    {
        static void Main(string[] args)
        {
            int IDKey = 1;
            List<SomeClass> UserData = new List<SomeClass>()
            {
                new SomeClass(),
                new SomeClass(1),
                new SomeClass(2)
            };
            //This operation actually works by evaluating the condition provided and adding the
            //object s if the bool returned is true, but we can do other things too
            UserData.Where(s => s.ID == IDKey).ToList();
            //We can actually write an entire method inside the Where clause, like so:
            List<SomeClass> filteredList = UserData.Where((s) => //Create a parameter for the func<SomeClass,bool> by using (varName)
                {
                    bool theBooleanThatActuallyGetsReturnedInTheAboveVersion =
                        (s.ID == IDKey);
                    if (theBooleanThatActuallyGetsReturnedInTheAboveVersion) s.name = "Changed";
                    return theBooleanThatActuallyGetsReturnedInTheAboveVersion;
                }
            ).ToList();

            foreach (SomeClass item in filteredList)
            {
                Console.WriteLine(item.name);
            }
        }
    }
    class SomeClass
    {
        public int ID { get; set; }
        public string name { get; set; }
        public SomeClass(int id = 0, string name = "defaultName")
        {
            this.ID = id;
            this.name = name;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间堆栈溢出
{
班级计划
{
静态void Main(字符串[]参数)
{
int-IDKey=1;
List UserData=新列表()
{
新建SomeClass(),
新类别(1),,
新课程(2)
};
//此操作实际上是通过评估提供的条件并添加
//如果返回的bool为true,则对象为s,但我们也可以执行其他操作
其中(s=>s.ID==IDKey.ToList();
//实际上,我们可以在Where子句中编写整个方法,如下所示:
List filteredList=UserData.Where((s)=>//使用(varName)为func创建一个参数
{
bool实际返回到上述版本的布尔值=
(s.ID==IDKey);
如果(实际获得上述版本的布尔值)s.name=“已更改”;
返回实际返回到上述版本的布尔值;
}
).ToList();
foreach(filteredList中的SomeClass项)
{
Console.WriteLine(项目名称);
}
}
}
上课
{
公共int ID{get;set;}
公共字符串名称{get;set;}
public SomeClass(int id=0,string name=“defaultName”)
{
this.ID=ID;
this.name=名称;
}
}
}

您不必重新插入它,反正您正在列表中直接处理它……不,我从这个列表中得到一个对象。我不是在做清单本身。我想删除/插入或更新该对象是的,但您拥有的对象是对列表中对象的引用,因此通过更新您找到的对象。您在哪里自动更新列表中的对象…用户数据包含哪些类型?它们是
还是
结构
?誓言这就容易了。谢谢