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#_Linq_List_Object_Search - Fatal编程技术网

C# 在列表中搜索匹配对象并指定值

C# 在列表中搜索匹配对象并指定值,c#,linq,list,object,search,C#,Linq,List,Object,Search,我的班级 class student { public string studentid { get; set; } public string groupid { get; set; }//Group id } 我的名单 List<student> pupils = new List<student>(); 我想通过一个for循环,它运行的次数等于studentsWithNoGroupId.Count,并用我的一些值分配组id。(仅显示如何分配

我的班级

class student   
{
    public string studentid { get; set; }
    public string groupid { get; set; }//Group id
}
我的名单

List<student> pupils = new List<student>();
我想通过一个for循环,它运行的次数等于studentsWithNoGroupId.Count,并用我的一些值分配组id。(仅显示如何分配每个对象就足够了)。 怎么做

还是我必须为此改变我的linq?
请有人帮帮我。

你是说这样的事吗

var studentsWithNoGroupId = 
from student in pupils
where student.groupid =="00"
select student;

foreach(var student in studentsWithNoGroupId)
{
  student.groupid = "x";
}
您的线路:

var studentsWithNoGroupId = from student in pupils
                            where student.groupid =="00"
                            select student;
创建Linq查询。此查询不会立即执行。要执行它,请尝试以下操作:

var studentsWithNoGroupIdQuery = from student in pupils
                                 where student.groupid =="00"
                                 select student;

var studentsWithNoGroupId = studentsWithNoGroupIdQuery.ToList();

foreach (var student in studentsWithNoGroupId)
{
    student.groupid = // Your logic here.
}

// Save pupils back to the database here.
ToList()使查询执行并将结果存储在
列表中。在迭代之前执行查询非常重要,因为您正在更改过滤查询的同一变量
groupid

     pupils.Where(p => p.groupid == "00")
      .ToList()
      .ForEach( 
         p => {
                p.groupid = "whatever";
         });
编辑(在Hatsjoem提示之后,更简单):


我不知道这是否是您想要的,但我会尝试一下:

var yourPupils = pupils.Where(p => p.groupid == "00")
    .Select(s => new student() { 
        studentid = s.studentid, 
        groupid = "yourCustomAssignedId" 
    }).ToList();
对于循环版本:

var numberOfPupils = pupils.Count();

for (int i = 0; i < numberOfPupils; i++)
{
    if (pupils[i].groupid == "00")
    {
        pupils[i].groupid = "yourCustomAssignedId";
    }
}
var numberofstudios=studios.Count();
for(int i=0;i
您不需要
where
只要选择,如果学生有正确的id,就返回学生,如果没有,就返回一个具有修改id的新学生(但这会返回一个事实,即linq是一种查询语言,您最好只使用普通的foreach编写)值在哪里?您想如何将这些分配给组SID?@Sayse No我已将没有组ID的学生分配为“00”,这就是我检查它的原因。。如何做任何代码示例..?@Tinwor我有max group id变量,我计划通过递增来分配每个组id。。这就是为什么我需要做一个for循环..我误解了这个问题。。价值观从何而来?其他地方的名单。ForEach linq很难看,2。如果您使用
FindAll
而不是
Where
,则不需要调用
ToList
方法,因为它已经返回一个列表。@Sayse:beauty is subjectiveLinQ是一个查询工具,而foreach linq在大多数情况下的使用方式对这一效果是不利的。是的。。我可能听起来很愚蠢。我对linq没有太多的了解。但是这个循环是否一直运行到studentsWithNoGroupID的对象数?谢谢你的帮助。我想得到studentsWithNoGroupId的计数怎么做…?如果我正确,你不能在
foreach
循环中赋值。你不能将student更改为新对象。更改其属性应该没有问题。如何将studentsWithNoGroupId的对象计数获取为int变量?我需要那个伯爵来决定其他一些事实。如何做到这一点。非常感谢您的帮助。@user3572467使用
studentsWithNoGroupId.Count
。非常感谢您的帮助非常感谢您的帮助。如果你能用for循环来实现,那就太好了。如果我能把你的瞳孔数变成一个变量。这是在
循环中进行的要求吗?如果不是的话,我个人认为这种方法更好。
var yourPupils = pupils.Where(p => p.groupid == "00")
    .Select(s => new student() { 
        studentid = s.studentid, 
        groupid = "yourCustomAssignedId" 
    }).ToList();
var numberOfPupils = pupils.Count();

for (int i = 0; i < numberOfPupils; i++)
{
    if (pupils[i].groupid == "00")
    {
        pupils[i].groupid = "yourCustomAssignedId";
    }
}