1

嗨,我正在尝试在下面的两个表上进行连接,但由于某种原因,我收到了一个错误

var query = (from a in pamsEntities.appraisals
             where a.emp_no == employeeId
             join b in pamsEntities.AppraisalReadinesses on a.AppraisalReadiness.Code equals b.Code
             select new { a, b }).ToList();

我收到错误无法转换匿名类型我对 LINQ 很陌生,所以不确定为什么会发生此错误。

对不起,这就是我厌倦了我不得不修改 telrik 报告的查询,以便我在连接中有描述,以便可以在报告中显示它

public List<appraisal> GetAppraisal(int employeeId)
    {
        List<appraisal> Appraisals = new List<appraisal>();

        try
        {

           var query = (from a in pamsEntities.appraisals
                         where a.emp_no == employeeId
                         join b in pamsEntities.AppraisalReadinesses on a.AppraisalReadiness.Code equals b.Code
                          select Tuple.Create(a, b)).ToList();
           Appraisals = query;//pamsEntities.appraisals.Where(a => a.emp_no == employeeId).ToList();
        }
        catch (Exception ex)
        {
            throw new EntityContextException("GetAppraisal failed.", ex);
        }


        return Appraisals;
    }

这是我要复制的 sql 查询

select a.*, b.*
from appraisls a , AppraisalReadiness b
where a.Readiness = b.code
and a.emp_no = emp_code;

where empcode is just a vairable

就是这样

4

1 回答 1

2

您的代码将结果投影到一系列匿名类型实例new { a, b }中,并且您正尝试将该列表用作其他内容的列表。将结果投影到正确的命名类型中。

例如,一种解决方案可能是

var query = (from a in pamsEntities.appraisals
             where a.emp_no == employeeId
             join b in pamsEntities.AppraisalReadinesses 
             on a.AppraisalReadiness.Code equals b.Code
             select Tuple.Create(a, b)).ToList();

这将为您提供一个列表,Tuple<T1, T2>其中列出了T1的类型aT2类型b。您当然可以创建自己的自定义类型:

class MyType
{
    // I am guessing about the type names here, based on your query
    Appraisal Appraisal; { get; set; }
    AppraisalReadiness Readiness; { get; set; }
}

并使用它

select new MyType { Appraisal = a, Readiness = b }
于 2013-08-09T08:31:15.380 回答