如何在Linq Lambda表达式中加入许多表?

时间:2016-03-16 06:24:43

标签: c# entity-framework linq lambda linq-to-entities

我有这个Lambda表达但不能正常工作。没有回复任何事情。你能帮助我吗:

    var query = db.Cheque
                    .Join(db.Contracts,
                    C => C.ContractIDRef,
                    Con => Con.ContractID,
                    (C, Con) => new { Cheques1 = C, Contracts1 = Con })
                    .Join(db.Parties,
                    Con => Con.Contracts1.ContractID,
                    Pt => Pt.ContractIDRef,
                    (Con, Pt) => new { Contract2 = Con, Parites1 = Pt })
                    .Join(db.Persons,
                    Pt => Pt.Parites1.PartyIDRef,
                    P => P.PersonID,
                    (Pt, P) => new { Parites2 = Pt, Persons1 = P })
                    .Join(db.Company,
                    Pt => Pt.Parites2.Parites1.CompanyIDRef,
                    Com => Com.CompanyID,
                    (Pt, Com) => new { Parites3 = Pt, Company1 = Com })
                    .Join(db.Bank,
                    C => C.Parites3.Parites2.Contract2.Cheques1.BankIDRef,
                    B => B.BankID,
                    (C, B) => new { Cheque2 = C, Bank1 = B })
                    .Join(db.Flats,
                    Con => Con.Cheque2.Parites3.Parites2.Contract2.Contracts1.FlatIDRef,
                    F => F.FlatID,
                    (Con, F) => new { Contract3 = Con, Flat1 = F })
                    .Join(db.Projects,
                    F => F.Flat1.ProjectIDRef,
                    Pr => Pr.ProjectID,
                    (F, Pr) =>
                    new
                    {
                        ChequeNumber = F.Contract3.Cheque2.Parites3.Parites2.Contract2.Cheques1.ChequeNo,
                        ChequeIDRef = F.Contract3.Cheque2.Parites3.Parites2.Contract2.Cheques1.ChequeIDRef,
                        ChequePrice = F.Contract3.Cheque2.Parites3.Parites2.Contract2.Cheques1.Amount,
                        BankName = F.Contract3.Bank1.BankName,
                        BranchName = F.Contract3.Cheque2.Parites3.Parites2.Contract2.Cheques1.BranchName,
                        ChequeDate = F.Contract3.Cheque2.Parites3.Parites2.Contract2.Cheques1.ChequeDate,
                        AccountNumber = F.Contract3.Cheque2.Parites3.Parites2.Contract2.Cheques1.AccNo,
                        AccountOwner = F.Contract3.Cheque2.Parites3.Parites2.Contract2.Cheques1.ChequeOwnerName,
                    }
                    )
`.Where(Total => SelectedChequesList.Contains(Total.ChequeIDRef.Value)).ToList();

1 个答案:

答案 0 :(得分:0)

我将从将上述内容转换为查询语法开始。虽然我是方法语法的粉丝,但在涉及多个连接的复杂查询中使用它是一个真正的痛苦。我需要花费大量时间阅读并尝试遵循上述查询,所以请这样做:

var query =
    from cheque in db.Cheque
    join contract in db.Contracts on cheque.ContractIDRef equals contract.ContractID
    join party in db.Parties on contract.ContractID equals party.ContractIDRef
    join person in db.Persons on party.PartyIDRef equals person.PersonID
    join company in db.Companies on party.CompanyIDRef equals company.CompanyID
    join bank in db.Bank on cheque.BankIDRef equals bank.BankID
    join flat in db.Flats on contract.FlatIDRef equals flat.FlatID
    join project in db.Projects on flat.ProjectIDRef equals project.ProjectID
    where SelectedChequesList.Contains(cheque.ChequeIDRef.Value)
    select new
    {
        ChequeNumber = cheque.ChequeNo,
        ChequeIDRef = cheque.ChequeIDRef,
        ChequePrice = cheque.Amount,
        BankName = bank.BankName,
        BranchName = cheque.BranchName,
        ChequeDate = cheque.ChequeDate,
        AccountNumber = cheque.AccNo,
        AccountOwner = cheque.ChequeOwnerName,
    };

现在,可以看到的一件事是大部分连接都没有被使用。但我们假设出于某些原因需要它们。请注意,所有连接都是INNER连接,因此如果没有匹配的记录,则任何连接都会导致查询返回空结果。

问题很可能出在这次加入

join person in db.Persons on party.PartyIDRef equals person.PersonID

而不是PartyIDRef我猜它应该像PersonIDRef