0

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

6
  • 4
    Why not just give them default value? Commented Jul 24, 2018 at 7:12
  • 1
    What have you tried? Why do you think a language-integrated query (that´s what linq stands for) would be the way to do this? Simply use a loop instead. Commented Jul 24, 2018 at 7:12
  • 3
    Why not just add a backing field for to the property and if this is null return 1 in the getter of AttendanceStatusId. Commented Jul 24, 2018 at 7:13
  • I am getting all records from server along with values so now I need to set the values. I tried loops but I though there might be easy solution using linq Thank you. Commented Jul 24, 2018 at 7:15
  • Don´t overcomplicate things with a technnique that isn´t made for updates but for querying. Just stay with your solution unless you have a real problem with it. Commented Jul 24, 2018 at 7:16

5 Answers 5

7

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; }
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

Definitly the best answer.
or to make it a bit simpler :) get { _attendanceStatusId ?? 1 }
@Nino I agree. I just wanted to show the null check in simple form. :) I will add it as comment.
public int? AttendanceStatusId { get; set; } = 1; will also do the trick.
@Guy If you accidentally set property to null it will no longer return 1.
|
1

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

Comments

1

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

Comments

1

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.