1

我不知道这个查询有什么问题 - 它一直给我这个错误:

Unable to create a constant value of type 'OvertimeProject.DataCore.tbl_Promotion'. Only primitive types or enumeration types are supported in this context.

这是我的 linq 声明:

public IEnumerable<StaffJoin> GetOrderStaffByDepartment(int DepID, DateTime DATE)
{
    using (var Context = new CRMDBEntities())
    {
        var result = (from pro2 in Context.tbl_Promotion
                      where pro2.PromotionDate <= DATE && pro2.DepID==DepID
                      select pro2).ToList();

        var result2 = (from Re in result
                      group Re by Re.StaffID into g2
                      join prop in Context.tbl_Promotion on g2.Max(c => c.PromotionID) equals prop.PromotionID
                      select prop).ToList();

        var result3 = (from s in Context.tbl_STaff
                       join
                       promotion in result2 on s.StaffID equals promotion.StaffID
                       join
                       position in Context.tbl_Position on promotion.PositionID equals position.PositionID
                       select new StaffJoin{Staff= s,Promotion= promotion,Position= position}).ToList();

        return result3;
    }
}

这是我想用返回的数据填充的课程:

 public class StaffJoin
 {
     public tbl_STaff Staff { get; set; }
     public tbl_Promotion Promotion { get; set; }
     public tbl_Position Position { get; set; }  
 }

...这是我的带有导航属性的模型结构:

tbl_Staff                  tbl_Promotion                 tbl_Position
---------                 --------------                 -------------
StaffID 0.1                PromotionID               0.1 PositionID
DepID                      PromotionDate                 PositionName
StaffName                 * StaffID                      PositionBase
Chek                       PositionID *                  Avaiilibilty
                           Avilibility
-------                   ---------------               --------------
Navigation                Navigation                    Navigation 
----------                --------------                ----------------
tbl_Department             tbl_Position                  tbl_Promotion
tbl_Promotion              tbl_Staff

我需要的是这个:

特定部门的所有员工加上特定日期后每位员工的最高晋升上限,以及与晋升相关的职位信息

4

1 回答 1

2

你的问题是你的代码基本上是这样的......

public IEnumerable<StaffJoin> GetOrderStaffByDepartment(int DepID, DateTime DATE)
{
    using (var Context = new CRMDBEntities())
    {
        List<Promotion> result = (from pro2 in Context.tbl_Promotion
                                where pro2.PromotionDate <= DATE && pro2.DepID==DepID
                                select pro2).ToList();

        List<Promotion> result2 = (from Re in result
                                group Re by Re.StaffID into g2
                                join prop in Context.tbl_Promotion on g2.Max(c => c.PromotionID) equals prop.PromotionID
                                select prop).ToList();

        var result3 = (from s in Context.tbl_STaff
                    join
                    promotion in result2 on s.StaffID equals promotion.StaffID
                    join
                    position in Context.tbl_Position on promotion.PositionID equals position.PositionID
                    select new StaffJoin{Staff= s,Promotion= promotion,Position= position}).ToList();

        return result3;
    }
}

现在在 result3 上,您尝试将两个List<Propmotion>放入 Linq 查询中,LinqToSQL 需要将其转换为 SQL。显然 SQL 不能处理 .net 用户类,所以它失败了。

如果你考虑杰夫的改变,你会得到

public IEnumerable<StaffJoin> GetOrderStaffByDepartment(int DepID, DateTime DATE)
{
    using (var Context = new CRMDBEntities())
    {
        IQueryable<Promotion> result = (from pro2 in Context.tbl_Promotion
                                where pro2.PromotionDate <= DATE && pro2.DepID==DepID
                                select pro2);

        IQueryable<Promotion> result2 = (from Re in result
                                group Re by Re.StaffID into g2
                                join prop in Context.tbl_Promotion on g2.Max(c => c.PromotionID) equals prop.PromotionID
                                select prop);

        var result3 = (from s in Context.tbl_STaff
                    join
                    promotion in result2 on s.StaffID equals promotion.StaffID
                    join
                    position in Context.tbl_Position on promotion.PositionID equals position.PositionID
                    select new StaffJoin{Staff= s,Promotion= promotion,Position= position}).ToList();

        return result3;
    }
}

现在,LinqToSQL 知道如何处理 IQueryables。它可以将结果和结果 2 中的 SQL 内联为子查询。因此差异。

但是,您需要更改的最后一件事是最后一行。IQueryable<T>代表一个查询,尚未发生的工作。当您调用时.GetEnumerator(),查询才会真正运行。但是,这只会发生在消费代码中,此时CRMDBEntities将被处置。呜呜……

public IEnumerable<StaffJoin> GetOrderStaffByDepartment(int DepID, DateTime DATE)
{
    using (var Context = new CRMDBEntities())
    {
        IQueryable<Promotion> result = (from pro2 in Context.tbl_Promotion
                                where pro2.PromotionDate <= DATE && pro2.DepID==DepID
                                select pro2);

        IQueryable<Promotion> result2 = (from Re in result
                                group Re by Re.StaffID into g2
                                join prop in Context.tbl_Promotion on g2.Max(c => c.PromotionID) equals prop.PromotionID
                                select prop);

        var result3 = (from s in Context.tbl_STaff
                    join
                    promotion in result2 on s.StaffID equals promotion.StaffID
                    join
                    position in Context.tbl_Position on promotion.PositionID equals position.PositionID
                    select new StaffJoin{Staff= s,Promotion= promotion,Position= position}).ToList();

        return result3.ToList();
    }
}

或者用vars 背...

public IEnumerable<StaffJoin> GetOrderStaffByDepartment(int DepID, DateTime DATE)
{
    using (var Context = new CRMDBEntities())
    {
        var result = (from pro2 in Context.tbl_Promotion
                                where pro2.PromotionDate <= DATE && pro2.DepID==DepID
                                select pro2);

        var result2 = (from Re in result
                                group Re by Re.StaffID into g2
                                join prop in Context.tbl_Promotion on g2.Max(c => c.PromotionID) equals prop.PromotionID
                                select prop);

        var result3 = (from s in Context.tbl_STaff
                    join
                    promotion in result2 on s.StaffID equals promotion.StaffID
                    join
                    position in Context.tbl_Position on promotion.PositionID equals position.PositionID
                    select new StaffJoin{Staff= s,Promotion= promotion,Position= position}).ToList();

        return result3.ToList();
    }
}
于 2013-05-09T02:48:17.797 回答