[英]Join 3 tables or more
I have a database and connected to Visual Studio 2017 with Entity Framework as model. 我有一个数据库,并以Entity Framework作为模型连接到Visual Studio 2017。 And on the database, there is some table which connect to other.
在数据库上,有一些表可以相互连接。 Like table
SELECTION_HISTORY
to table USER
and to CANDIDATE
. 将表
SELECTION_HISTORY
类似于表USER
和CANDIDATE
。 But how to connect those tables in C#? 但是如何在C#中连接这些表呢?
I was trying to connect 2 tables and it works, but when I try to connect 3 tables, there is some problem with my code. 我试图连接2个表,但它可以工作,但是当我尝试连接3个表时,我的代码存在一些问题。
List<CandidateInterviewDTO> CandidateInterview = db.CANDIDATEs.Join(db.POSITIONs,
candidate => candidate.JUDUL_POSISI,
position => position.POSITION_ID,
(candidate, position) =>
new CandidateInterviewDTO
{
AppliedPosition = db.POSITIONs.FirstOrDefault(p => p.POSITION_ID == candidate.JUDUL_POSISI).POSITION_NAME,
SuitablePosition = db.POSITIONs.FirstOrDefault(p => p.POSITION_ID == candidate.SUITABLE_POSITION).POSITION_NAME,
Name = candidate.NAMA_LENGKAP,
Source = db.SOURCEs.FirstOrDefault(s => s.SOURCE_ID == candidate.SOURCE_ID).SOURCE_NAME,
PhoneNumber = candidate.NOHP,
Email = candidate.EMAIL
// If i try until this, its work. But when i try the code on below, there is some error with my code. Is it correct ?
}).Join(db.SELECTION_HISTORY,
candidatePosisition => candidatePosisition,
sh => sh.PIC_ID, (candidatePosition, selection) => new CandidateInterviewDTO
{
Name = db.CANDIDATEs.FirstOrDefault(c => c.NAMA_LENGKAP == candidatePosition.Name).NAMA_LENGKAP,
CandidateId = db.SELECTION_HISTORY.FirstOrDefault(sh => sh.PIC_ID == selection.PIC_ID).PIC_ID,
});
The error is 错误是
CS0411 The type arguments for method 'Queryable.Join<TOuter, TInner, TKey, TResult>(IQueryable<TOuter>, IEnumerable<TInner>, Expression<Func<TOuter, TKey>>, Expression<Func<TInner, TKey>>, Expression<Func<TOuter, TInner, TResult>>)' cannot be inferred from the usage.
CS0411方法'Queryable.Join <TOuter,TInner,TKey,TResult>(IQueryable <TOuter>,IEnumerable <TInner>,Expression <Func <TOuter,TKey >>,Expression <Func <TInner,TKey >>,无法从用法中推断出表达式<Func <TOuter,TInner,TResult >>)'。 Try specifying the type arguments explicitly
尝试显式指定类型参数
You are invoking Join
, and then chaining the result to another Join
invocation. 您正在调用
Join
,然后将结果链接到另一个Join
调用。
The first call is returning a result of type IQueryable<CandidateInterviewDTO>
. 第一次调用返回一个
IQueryable<CandidateInterviewDTO>
类型的结果。
So when you invoke the second join, the second and third parameters are lambdas that return the keys that are being compared. 因此,当您调用第二个联接时,第二个和第三个参数是返回要比较的键的lambda。 They are returning different types.
他们正在返回不同的类型。 - One type is of type
CandidateInterviewDTO
- The other type is whatever PIC is. -一种类型是
CandidateInterviewDTO
类型-另一种类型是PIC。
It's typical to prefer call-chaining to LINQ Query Syntax. 与LINQ查询语法相比,通常更喜欢调用链接。 However,when it comes to joining, Query Syntax absolutely shines.
但是,当涉及到联接时,查询语法绝对令人眼前一亮。
Here's how you can approach the three way join using Query Syntax: 这是使用查询语法进行三种方式联接的方法:
var candidateInterviews = from candidate in db.CANDIDATES
join position in db.POSITIONS
on candidate.JUDUL_POSISI equals position.POSITION_ID
join selectionHistory in db.SELECTION_HISTORY
on position.POSITION_ID equals selectionHistory.PID_ID
select new CandidateInterviewDTO
{
Name = candidate.NAMA_LENGKAP,
(...)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.