使用JOIN的ADO.NET实体模型和LINQ

时间:2018-06-13 15:45:45

标签: c# mysql linq join entity-framework-6

我的查询:

using (var context = new consoleApp.JohnDoe_dbEntities())
        {
            var data= (from lo in context.tbl_loans
                         join doc in context.tbl_docinfo on lo.idDocInfo equals doc.idDocInfo
                         join insp in context.tbl_inspections on lo.idLoan equals insp.idLoan
                         where insp.StatusInspections == 3 && lo.idLoan == 5
                         select new
                         {
                             lo,
                             doc,
                             insp
                         });}

我想访问tbl_idLoans,tbl_docinfo和tbl_inspections示例中的每一列:“lo.CityName”。我不明白我如何使用LINQ语法访问它们。当前查询是正确的,除了SELECT部分​​以后,我不知道它是否正在做我想要的,这使我可以访问条件适用的所有三个表上的每一列。

感谢。

1 个答案:

答案 0 :(得分:0)

select语句中为表属性指定名称。通过这样做,您应该能够使用语法datadata.FirstOrDefault().Inspection.StatusInspections等从data.FirstOrDefault().Loan.idLoan对象的任何表中选择一列。

var data = (from lo in context.tbl_loans
            join doc in context.tbl_docinfo on lo.idDocInfo equals doc.idDocInfo
            join insp in context.tbl_inspections on lo.idLoan equals insp.idLoan
            where insp.StatusInspections == 3 && lo.idLoan == 5
            select new
            {
                Loan = lo,
                DocInfo = doc,
                Inspection = insp
            });