Linq - Inner Join不从左表中检索所有记录

时间:2014-10-07 11:02:28

标签: c# linq entity-framework

我已经完成了内部联接,它只显示了以下查询的匹配记录: -

var data = from t1 in ctx.tblEmp
           join t2 in ctx.tblHelp 
               on t1.Field equals t2.Fld
           where t1.Id == Id && 
                 t2.Id == Id
           select new Settings { Master = t1.Labelname, OrderNo = t2.OrderNo};

我想要来自tblEmp&的所有记录仅匹配tblHelp的记录。

怎么做?

2 个答案:

答案 0 :(得分:2)

用作

 var data = from t1 in ctx.tblEmp
       join t2 in ctx.tblHelp 
           on t1.Field equals t2.Fld into u
           from t2 in u.DefaultIfEmpty()
       where t1.Id == Id && 
             t2.Id == Id

       select new Settings { Master = t1.Labelname, OrderNo = t2.OrderNo};

试试这个

 var data = from t1 in ctx.tblEmp
       join t2 in ctx.tblHelp 
           on t1.Field equals t2.Fld into u
           from t2 in u.DefaultIfEmpty()
       where t1.Id == Id 
       orderby columnname // Added Order By
       select new Settings { Master = t1.Labelname, OrderNo = t2.OrderNo==null ?"":t2.OrderNo};

答案 1 :(得分:1)

    var query = from t1 in ctx.tblEmp
                    join t2 in ctx.tblHelp on t1 equals t2.Fld into tempGroup
                    from subpet in tempGroup.DefaultIfEmpty()
                    select new { t1.Labelname, OrderNo = (subpet == null ? String.Empty : subpet.OrderNo) };