none
Left Join with grouping PROBLEM RRS feed

  • Question

  • Hi all,

    I'm a newbie in entity framework.

    I hope anyone could help me with my problem, thanks a lot before.

     

    This is my scenario :

    I have 2 tables

    good_master and good_batch as an ilustration I just show important field.

    good_master [ id, code, .. ]

    good_batch[id, good_id, batchno ,qty ]

     

    I want to display something like this:

    GOOD | STOCK

    A         |   15

    B         |   10

     

    SO , what I do is :

    I group a good_batch based on good_id, like this :

    var gbGroup = from gb in ctx.ObjectContext.good_batch
                                       group gb by gb.good_id into batch
                                       select new
                                       {
                                           good_id = batch.Key,
                                           stock = (from gb2 in batch
                                                    select gb2.qty).Sum()
                                       };

    and then I want to use that to joined with good_master like this :

    var data = from t in ctx.ObjectContext.good_master
                                   join b in gbGroup on t.id equals b.good_id into tb
                                   from tb2 in tb.DefaultIfEmpty()
                                   where t.name_ind.Contains(name_ind) && t.enabled == true
                                   select new GoodData
                                   {
                                       id = t.id,
                                       code = t.code,
                                       name_ind = t.name_ind,
                                       stock = ( tb2 == null ? 0 : tb2.stock )     // <<<<<<<<<<<<<<<<< HERE HERE HERE
                                   };

    what i want to ask is :
    Why ( tb2 == null ? 0 : tb2.stock ) is not worked ( Based on my browsing on inet, i found a solution like that )

    BUT here I got an error :

    dbisnullexpression must refer to a primitive or reference type

    Could anyone help me ??

     

    thanks a lot

    God Bless You

     

     

    Stefanus

     

     

     

     

    Friday, January 13, 2012 1:37 PM

Answers