如果使用Linq值为null,则更新列表列的值

时间:2018-07-24 07:10:22

标签: c# list linq

我有List<Student>,其中有多条记录

public class Student
{            
    public int? StudentId { get; set; }
    public string StudentName { get; set; }
    public int? AttendanceStatusId { get; set; }
    public string AttendanceStatusDes { get; set; }     
}

在将数据发布到服务器时,有时 AttendanceStatusId AttendanceStatusDes null。 当这两个值在列表记录中为空时,我要设置AttendanceStatusId=1AttendanceStatusDes="Present" 我如何使用Linq查询做到这一点。 谢谢

5 个答案:

答案 0 :(得分:7)

为模型设置默认值,而不是使用LINQ或循环。这样,您将始终确保可以按预期方式获得模型,而无需在所有学生列表中四处乱逛。

这可能不适用于OP情况,但值得一提的是,如果您的类可序列化,则使用此“技术”仍然可以得到默认值。

public class Student
{
    public int? StudentId { get; set; }
    public string StudentName { get; set; }
    private int? _attendanceStatusId;
    public int? AttendanceStatusId
    {
        //you can use null coalescing operator here
        //like this: get { return _attendanceStatusId ?? 1; }
        get { return _attendanceStatusId == null ? 1 : _attendanceStatusId; }
        set { _attendanceStatusId = value; }
    }
    private string _attendanceStatusDes;
    public string AttendanceStatusDes
    {
        //Or get { return _attendanceStatusDes ?? "Present" }
        get { return _attendanceStatusDes == null ? "Present" : _attendanceStatusDes; }
        set { _attendanceStatusDes = value; }
    }
}

答案 1 :(得分:1)

获取过滤列表:

var missingStatus = Students.Where(s => !s.AttendanceStatusId.HasValue && AttendanceStatusDes == null);

然后遍历该列表,编辑这些项目上的值。

foreach(var student in missingStatus)
{
  student.AttendanceStatusId = 1;
  student.AttendanceStatusDes = "Present";
}

有帮助吗?

您当然可以在列表中的foreach中做到这一点,但我不建议这样做:

Students.Where(s => !s.AttendanceStatusId.HasValue && AttendanceStatusDes == null)
  .ToList()
  .ForEach(s => {
      s.AttendanceStatusId = 1;
      s.AttendanceStatusDes = "Present";
  });

答案 2 :(得分:1)

只需设置默认值,如果您使用的是C#6+,则可以执行以下操作:

class Student
{
    public int? StudentId { get; set; }
    public string StudentName { get; set; }
    public int AttendanceStatusId { get; set; } = 1;
    public string AttendanceStatusDes { get; set; } = "Present";
}

答案 3 :(得分:1)

我想我会遵循以下原则:

Students.Select(t=>new Student{
AttendanceStatusId = t.AttendanceStatusId.HasValue?t.AttendanceStatusId.Value:1, 
AttendanceStatusDes = t.AttendanceStatusId.HasValue?t.AttendanceStatusDes : "Present" , 
StudentId = t.StudentId,
StudentName = t.StudentName
});

答案 4 :(得分:0)

[Route("api/Sample/Murugan")]
        [HttpGet]
        public string Name()
        {
            List<Student> obj = new List<Student> {
                new Student { AttendanceStatusDes=null, AttendanceStatusId =null, StudentId =1, StudentName ="A" },
                new Student { AttendanceStatusDes="", AttendanceStatusId =1, StudentId =2, StudentName ="B" },
                new Student { AttendanceStatusDes="", AttendanceStatusId =2, StudentId =3, StudentName ="C" },
                new Student { AttendanceStatusDes=null, AttendanceStatusId =null, StudentId =4, StudentName ="D" },
            };

            for (int Count = 0; Count < obj.Count(); Count++)
            {
                if (obj[Count].AttendanceStatusId == null && obj[Count].AttendanceStatusDes == null)
                {
                    obj[Count].AttendanceStatusId = 1;
                    obj[Count].AttendanceStatusDes = "Present";
                }
            }
            return "Muruganvc";
        }