none
Coalesce on an entity RRS feed

  • Question

  • Hi,

    let's say that i have this simple join query

    var lst = from pat in ctx.TMP_PATIENTS
                              join cons in ctx.T_CONSULTATIONS.Include(a => a.T_EXAMENS)
                              on pat.ID_PATIENT equals cons.CON_ID_PATIENT into joinpatcons
                              from p in joinpatcons.DefaultIfEmpty()
    
                              select new
                              {
                                  patients = pat,
                                  consultation = p,
                                  CON_DATE = p.CON_DATE
                              };
    

    What i want is to select CON_DATE only if (p != null )

    any idea please ?

    Thursday, January 28, 2016 2:17 PM

All replies

  • Hi issam1975,

    According to your code snippet, it seems a left outer joins LINQ expression. As your requirement, it seems you need an inner join LINQ expression. You could try to modify your code like below.

    var lst = from pat in ctx.TMP_PATIENTS
    
              join cons in ctx.T_CONSULTATIONS.Include(a => a.T_EXAMENS)
    
              on pat.ID_PATIENT equals cons.CON_ID_PATIENT 
    
              select new
    
              {
    
                  patients = pat,
    
                  consultation = p,
    
                  CON_DATE = p.CON_DATE
    
              };
    

    In addition, you could set CON_DATE a null value if p is null, and you could refer to the follow code.

    var lst = from pat in ctx.TMP_PATIENTS
    
              join cons in ctx.T_CONSULTATIONS.Include(a => a.T_EXAMENS)
    
              on pat.ID_PATIENT equals cons.CON_ID_PATIENT into joinpatcons
    
              from p in joinpatcons.DefaultIfEmpty()
    
              select new
    
              {
    
                  patients = pat,
    
                  consultation = p,
    
                  CON_DATE = (p == null ? null : p.CON_DATE) 
    
              };
    

    I hope it’s helpful to you.

    Best regards,

    Cole Wu

    Friday, January 29, 2016 5:11 AM
    Moderator