1. 程式人生 > >Linq Join 三表查詢

Linq Join 三表查詢

   public static IQueryable<ShoppingCarView> CreateView(int type)
        {
            var context = new RookieFContext(AtawAppContext.Current.DefaultConnString);
            IQueryable<ShoppingCarView> view;
           
             if (type == (int)RookieFShoppingCarType.RookieFMall)
             {
                 view = context.RF_TASHOPINGCAR.Join(context.RF_MALLGOODS,
                   a => a.RT_DISHESID, b => b.FID, (a, b) => new ShoppingCarView()
                   {
                       RF_TASHOPINGCAR = a,
                       RF_MALLGOODS = b

                   });
             }else
                 view = from o in context.RF_TASHOPINGCAR
                        join p in context.RF_TAMENU on o.RT_DISHESID equals p.FID into ords
                        join d in context.RF_TALOVEPROGRAM on o.RT_DISHESID equals d.FID into programs
                        select new ShoppingCarView()
                        {
                            RF_TASHOPINGCAR= o,
                            RF_TAMENU = ords.FirstOrDefault(),
                            RF_TALOVEPROGRAM = programs.FirstOrDefault()
                        };
            return view;
        }