1

我正在尝试将我的 sql commang 写入 Linq:

SQL:

select avg(sub.evaluation) from submit_task sub where student_id='" + idStudent + "' and state='close';

林克:

double avg = (ado.submit_task.Where(r => (r.id == idStudent && r.state == "close")).Average(r => r.evaluation));

avgStudent = avg.ToString();

但这不起作用,当我删除&& r.state == "close"语句时,我得到了结果,但它不正确。

谢谢你。

4

2 回答 2

3

我已经用一组样本数据尝试过同样的方法,它工作正常

 List<student> students = new List<student>
        {
            new student{id="1",state="close",evaluation=5},
            new student{id="1",state="close",evaluation=4}
        };
        double avg = (students.Where(r => (r.id == "1" && r.state == "close")).Average(r => r.evaluation));

public class student
{
    public string id { get; set; }
    public string state { get; set; }
    public int evaluation { get; set; }
}

可能是您应该检查数据库中的数据或修改state="close"查询的一部分

于 2013-05-05T12:16:24.927 回答
0

好的,这是代码,它的工作原理:

var avgEvalClose = (from sub in ado.submit_task
                    where sub.student_id.Equals(idStudent)
                    where sub.state.Equals("close")
                    select sub.evaluation).Average();
avgStudent = avgEvalClose.ToString();
于 2013-05-05T17:08:34.477 回答