Arvind Chourasiya
Arvind Chourasiya

Reputation: 17482

Update List column values, if value is null using Linq

I have List<Student> with multiple records

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

While posting data to server, sometimes AttendanceStatusId & AttendanceStatusDes going null. When these 2 values are null in List records, I want to set AttendanceStatusId=1 & AttendanceStatusDes="Present" How could I do that using Linq query. Thank you

Upvotes: 0

Views: 3935

Answers (5)

Renatas M.
Renatas M.

Reputation: 11820

Instead of using LINQ or loops, set defaults for your model. This way you will be always sure you get your model as expected and you don't need to mess around everywhere with all your list of Students.

This might not be applicable for OPs case but worth to mention, that using this "technique" you still get default values if your class is serializable.

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

Upvotes: 7

Gautham Srinivasan
Gautham Srinivasan

Reputation: 308

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

Upvotes: 0

Umang
Umang

Reputation: 875

I think I would go with following:

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

Upvotes: 1

Maciej Pulikowski
Maciej Pulikowski

Reputation: 2527

Just set default value, If you are using C# 6+ you can do this:

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

Upvotes: 1

Mafii
Mafii

Reputation: 7465

Get the filtered list:

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

And then iterate over that list, editing the values on these items.

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

Does that help?

You can of course do it in a foreach on a list, but I don't recomment that:

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

Upvotes: 1

Related Questions