无法将类型'AnonymousType#1 []'隐式转换为'double []

时间:2013-12-31 09:25:54

标签: entity-framework

我正在编写在lambda表达式中封装查询的函数,我想以数组的形式返回查询结果,但它在最后一行代码中给出了错误: - 错误如下: -

无法将类型'AnonymousType#1 []'隐式转换为'double []

查询以下是在最后一行代码上给出上述错误:

 public double[] PopulateGrid()
    {
        using (var db = new hdContext())
        {
            var query = db.tickets.Join(
                db.employees, t => t.CreatedBy, e => e.EmployeeId, (t, e) => new
                {
                    t.Id,
                    t.EmployeeName,
                    e.Employee_Name,
                    e.Department,
                    e.Location,
                    t.Subject,
                    t.MainCatId,
                    t.SubCatId,
                    t.PickTicket,
                    t.Status,
                    t.CreateDate
                }).Join
                (
                db.MainCategory, t => t.MainCatId, m => m.MainCatId, (t, m) => new
                {
                    t.Id,
                    t.EmployeeName,
                    t.Employee_Name,
                    t.Department,
                    t.Location,
                    t.Subject,
                    t.MainCatId,
                    m.MainCatName,
                    t.SubCatId,
                    t.PickTicket,
                    t.Status,
                    t.CreateDate
                }).ToArray();

            return query;

        }

    }

1 个答案:

答案 0 :(得分:0)

目前尚不清楚你要做什么。 Join方法的最后一个参数是结果选择器(它确定查询返回的内容),在您的情况下,您将返回匿名类型:

new
{
    t.Id,
    t.EmployeeName,
    t.Employee_Name,
    t.Department,
    t.Location,
    t.Subject,
    t.MainCatId,
    m.MainCatName,
    t.SubCatId,
    t.PickTicket,
    t.Status,
    t.CreateDate
}

显然,您无法将此类型转换为double,因此您可能希望在使用.Select(z => z.MyDoubleField)实现实体之前使用ToArray()选择特定字段