Condition check in Linq Query- Linq

Jul 13, 2019 BY xtreme

Check if Match Found
public Student GetStudentDetails(int id)
{
	using (DbContextModel _db = new DbContextModel())
	{
		var result = _db.student.Where(x => x.is_active && !x.is_deleted).Select(x => new StudentModel
		{
			id = x.id,
			mode = x.status.ToString().Contains("paid") == true ? "Paid" : "Pending",
		}).FirstOrDefault();

		return result;
	}
}
Check Nullable Data using Ternary Operator in Linq
public Student GetStudentDetails(int id)
{
	using (DbContextModel _db = new DbContextModel())
	{
		var result = _db.student.Where(x => x.is_active && !x.is_deleted).Select(x => new StudentModel
		{
			id = x.id,
			name = x.name != null ? x.parent.name : "",
		}).FirstOrDefault();

		return result;
	}
}


Related