我创建了三个表名:emps,emp_project,emp_location
现在我想从每个表中选择一列,但到目前为止我执行连接查询时会出现此错误:
join子句中某个表达式的类型不正确。 调用' GroupJoin'。
时,类型推断失败
我执行的查询是:
from e in Emp_info
from p in Emp_projects
join l in Emp_locations
on new { e.User_id , p.Project_id } equals new { l.User_id, l.Project_id } into detail
from l in detail
select new
{
e.Middlename,
p.Project_name,
l.Location
};
query.Dump("Join query");
不知道哪个条款导致错误!
答案 0 :(得分:0)
我的猜测是,它试图比较的两个匿名类型是不一样的(也确保属性是相同的数据类型)。
更改
on new { e.User_id , p.Project_id } equals new { l.User_id, l.Project_id } into detail
要
on new { UserId = e.User_id, ProjectId = p.Project_id } equals
new { UserId = l.User_id, ProjectId = l.Project_id } into detail
答案 1 :(得分:0)
这对我有用
void Main()
{
var Emp_info = new List<Info>
{
new Info {Middlename = "Middlename",User_id = "1"}
};
var Emp_projects = new List<Project>
{
new Project{Project_id = "1",Project_name = "Project"}
};
var Emp_locations = new List<LocationInfo>
{
new LocationInfo{Location = "Location",Project_id="1",User_id = "1"}
};
/* your code */
var query = from e in Emp_info
from p in Emp_projects
join l in Emp_locations
on new { e.User_id , p.Project_id } equals new { l.User_id, l.Project_id } into detail
from l in detail
select new
{
e.Middlename,
p.Project_name,
l.Location
};
query.Dump("Join query");
/* your code */
}
class Info
{
public string User_id;
public string Middlename;
}
class Project
{
public string Project_id;
public string Project_name;
}
class LocationInfo
{
public string User_id;
public string Project_id;
public string Location;
}
答案 2 :(得分:0)
到目前为止,我已经通过一次使用多个连接来实现此解决方案。 不是正确的做法。
var query=
from e in Emp_info
join l in Emp_locations on e.User_id equals l.User_id // first join
join p in Emp_projects on l.Location_id equals p.Project_id // second join
select new
{
Name= e.Middlename,
Project = p.Project_name,
Location = l.Location
};
query.Dump();
答案 3 :(得分:0)
终于得到了答案..这完全符合我的要求
var query=
from e in db.emp_info
from p in db.emp_projects
join l in db.emp_locations
on new { username= e.User_id ,project_id=p.project_id } equals new { username=l.User_id,project_id= l.Project_id } into detail
from l in detail
select new
{
e,p,l
};
foreach (var q in query)
{
Console.WriteLine("{0} has done project like {1} in {2}",q.e.Middlename,q.p.project_name,q.l.location);
}
Console.ReadLine();
}
Console.ReadLine();