首页 > 解决方案 > 如何使用 web api 从两个表中获取数据

问题描述

我想使用 web api 从两个表中获取数据,但我收到一个错误。

这是我的代码:

public List<tblEmpTask> GettblEmpTasks()
        {
var q = (from n in db.tblEmpTasks
                     join c in db.tblEmployees on n.intEmpCode equals c.intEmpCode

                     select new
                     {
                         n.strTaskName,
                         n.dtStart,
                         c.strEmployeeName,

                     }).ToList();
            return q;
}

这是我收到的错误:

  The type of one of the expressions in the join clause is incorrect.  Type inference failed in the call to 'Join'.

请注意,我使用的是 web api。期待解决这个问题。

标签: model-view-controllerasp.net-web-api2

解决方案


选择匿名函数时,该函数应返回List<object> ,或者改为 使用类型返回值,例如 -

    class myType{
            string TaskName,     //assuming type as string
            string dtStart,     //assuming type as string
            string EmployeeName //assuming type as string
    }

并在 LinQ 查询中类似于 -

select new myType{ TaskName=n.strTaskName, dtStart=n.dtStart, EmployeeName=c.strEmployeeName}

并返回为List<myType>


推荐阅读