0

每当我尝试运行以下代码时

        employees = from e in this.entities.employees select e;
        var ett = (from e in employees where 
                      (e.salaries.ToList().Where(i => i.to_date.Year == 9999).First().salary1 > TT) 
                  select e);

        var etf = (from e in employees
                  where
                      (e.salaries.ToList().Where(i => i.to_date.Year == 9999).First().salary1 < TT &&
                      e.salaries.ToList().Where(i => i.to_date.Year == 9999).First().salary1 > TF)
                  select e);

        foreach (employee e in ett)
        {
            this.members.memberships.AddObject(new membership
            {
                FirstName = e.first_name,
                LastName = e.last_name,
                MembershipClass = "regular",
                VerificationCode = "abc1234"
            });
        }

        foreach (employee e in etf)
        {
            this.members.memberships.AddObject(new membership
            {
                FirstName = e.first_name,
                LastName = e.last_name,
                MembershipClass = "regular",
                VerificationCode = "abc1234"
            });
        }

我收到以下错误:

LINQ to Entities does not recognize the method 'System.Collections.Generic.List`1[app.salary] ToList[salary](System.Collections.Generic.IEnumerable`1[app.salary])' method, and this method cannot be translated into a store expression.

Employee 类确实与薪水保持一对多的关系。据我所知,错误指的是 .ToList() 方法调用,但删除它们并没有解决问题。

编辑:添加了更多代码,希望对您有所帮助。

4

1 回答 1

0

我认为这部分:e.salaries.ToList()是问题所在。您不必.ToList()做 linq 查询,请尝试不使用.ToList()

var ett = (from e in employees where 
                      (e.salaries.Where(i => i.to_date.Year == 9999).First().salary1 > TT) 
                  select e);

    var etf = (from e in employees
              where
                  (e.salaries.Where(i => i.to_date.Year == 9999).First().salary1 < TT &&
                  e.salaries.Where(i => i.to_date.Year == 9999).First().salary1 > TF)
              select e);
于 2013-05-02T02:26:33.253 回答