简体   繁体   English

参加3张或更多桌

[英]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类似于表USERCANDIDATE 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.

相关问题 用“ not in”子句连接两个以上的表 - Join more than two tables with “not in” clause LINQ中有超过2个表JOIN,作用域问题 - More than 2 tables JOIN in LINQ, scope issue Sql加入两个以上的表 - Sql join more than two tables (Linq/Lambda) 将 2 个或多个表与 2 个 DBContext 连接起来 - (Linq/Lambda) Join 2 or more tables with 2 DBContext LINQ使用具有多个(4)或更多表的多个.join扩展名 - LINQ using multiple .join extensions with multiple (4) or more tables 如何使用 linq 在 2 个以上的表上使用左连接和非左连接 - How do I use a left-join and non-left-join on more than 2 tables using linq 如何在实体框架中连接两个或多个表并在数据网格视图中显示每个表中的选定列 - How to join two or more tables in entity framework and display selected columns from each tables in data grid view 尝试使用defaultifempty方法连接两个以上的表时,linq表达式中出现空引用错误 - Getting null reference error in linq expression when trying to join more than 2 tables using defaultifempty method 如何在LINQ中对两个以上的表进行外部联接? - How can I do an outer join to more than two tables in LINQ? 如何使用Join with Entity Framework Lambda和Linq在两个或多个表中进行搜索 - How to do a search in two or more tables using Join with Entity framework Lambda and Linq
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM