Linq TO EF联合查询JOIN简单示例

  var result = ef.M_Record.Where(where).GroupBy(g => new { g.F_FireID})
                .Select(s => new { MR_ID =s.Max(m=>m.MR_ID)})
                .Join(ef.M_Record,
                    L => E.MR_ID,
                    R => F.MR_ID, (L, R) => new 
                    {
                        F_FireID = R.F_FireID,
                        MR_ID = R.MR_ID,
                        MR_Type = R.MR_Type,
                    }).OrderByDescending(o => o.MR_ID).ToList();


    //左连接查询体       
     var result = ef.F_CityFireAsk.Where(p => p.F_FireID == fireID)
                .Select(s => new { s.CFA_ID, s.F_FireID, s.CFA_AskContent, s.CFA_HandleTime })
                .GroupJoin(ef.F_CountyFireAnswer,
                    E => E.CFA_ID,
                    F => F.CFA_ID, (E, F) => new
                    {
                        E, F
                    }).Select(s => new {
                        CFA_ID = s.E.CFA_ID,
                        F_FireID = s.E.F_FireID,
                        CFA_AskContent = s.E.CFA_AskContent,
                        CFA_HandleTime = s.E.CFA_HandleTime,
                        Cou_AnswerContent = s.F.FirstOrDefault()!=null ? s.F.FirstOrDefault().Cou_AnswerContent:null,
                        Cou_Answerer = s.F.FirstOrDefault() != null ? s.F.FirstOrDefault().Cou_Answerer : null,
                        Cou_HandleTime = s.F.FirstOrDefault() == null ? new Nullable<System.DateTime>(): s.F.FirstOrDefault().Cou_HandleTime
                    })
                    .OrderBy(o => o.CFA_ID).ToList();

最新回复(0)
/jishu5dlCPI2ZLghoMkKuEBqifc4OLWea_2BGWf8qFCthGXgZs_3D2521375
8 简首页