c#,如何在Linq中调用计算年龄和调用方法,Windows窗体

时间:2019-04-19 10:28:44

标签: c# entity-framework

当我以dataGridView表单加载员工时加载所有员工时,我会收到此错误消息。

  

“ LINQ to Entities无法识别方法'int32 CalculateAge(Int32)'方法,并且该方法无法转换为商店表达式”

这就是我在dataGridView中填充它们的方式

private void LoaddEmployees() {
   try
    {
        db = new EmployeeEntities();

        var employees = (from u in db.Users 
                         select new
                         {
                             EmployeeId = u.EmployeeId,
                             UserName = u.UserName,
                             FirstName = u.FirstName,
                             LastName = u.LastName,
                             Birthday = u.Birthday,
                             Age = CalculateAge(u.EmployeeId) // Calling CalculateAge method
                         }).ToList();
        dgvEmployeesList.DataSource = employees;                  
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
} 

这是我遇到问题的CalculateAge方法。真正的计算(数学)工作正常,但是在加载dataGridview时出错。

 private int CalculateAge(int employeeId) {

    int age;
    var employeeAge = db.Users.Where(x => x.EmployeeId == employeeId).FirstOrDefault();

            DateTime empBirthday = Convert.ToDateTime(empAge.Birthday);
            DateTime today = DateTime.Today;
            age = today.Year - empBirthday.Year;
            if (empBirthday > today.AddYears(-age)) 
                age--;
                return age; 
}

页面加载时出现上述错误:

请为您解决此问题。我真的不明白我的代码有什么问题。

2 个答案:

答案 0 :(得分:0)

 var employees = (from u in db.Users 
                                 select new
                                 {
                                     EmployeeId = u.EmployeeId,
                                     UserName = u.UserName,
                                     FirstName = u.FirstName,
                                     LastName = u.LastName,
                                     Birthday = u.Birthday,
                                     Age = CalculateAge(u.EmployeeId) // Calling CalculateAge method
                                 })

您的代码的这一部分已通过Entity Framework转换为SQL查询,可惜您的C#方法CalculateAge(u.EmployeeId)无法转换为SQL。 U不能将这种自定义方法与IQueriable一起使用。首先检索数据,然后应用方法或在SQL端进行相同的计算(例如存储过程)。

答案 1 :(得分:0)

您可以将db.Users转换为list,然后运行评估您的年龄计算。代码看起来像

private void LoaddEmployees()
    {
        try
        {
            db = new EmployeeEntities();

            var employees = (from u in db.Users.ToList()
                             select new
                             {
                                 EmployeeId = u.EmployeeId,
                                 UserName = u.UserName,
                                 FirstName = u.FirstName,
                                 LastName = u.LastName,
                                 Birthday = u.Birthday,
                                 Age = CalculateAge(u.Birthday) // note that we are sending in the Birthday
                             }).ToList();
            dgvEmployeesList.DataSource = employees;


        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

然后将CalculateAge方法更新为

private int CalculateAge(string birthday)
    {
        if (string.IsNullOrWhiteSpace(birthday)) return 0;

        DateTime empBirthday = Convert.ToDateTime(birthday);
        DateTime today = DateTime.Today;
        age = today.Year - empBirthday.Year;
        if (empBirthday > today.AddYears(-age))
            age--;
        return age;
    }