Casting in Linq Query - Linq

Jul 11, 2019 BY xtreme

Cast double via 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,
			amount = _db.payments.Select(z => (double?)z.amount.Value).FirstOrDefault(),          
		}).FirstOrDefault();

		return result;
	}
}
Cast double using local function via 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,
			amount = _db.payments.Where(z => z.id == x.id).Select(x => CalculateAmount(Convert.ToDouble(x.amount))).Sum(),          
		}).FirstOrDefault();

		return result;
	}
}
Related