Tuesday, June 18, 2019

Fill Model List or Fill Class List with join and left join class in Linq

1.Class

    [MetadataType(typeof(auth_model_mas_meta))]
    public partial class auth_model_mas
    {
        public string model_id { get; set; }
        public string model_name { get; set; }
        public Nullable<bool> model_st { get; set; }
    }

   [MetadataType(typeof(auth_form_mas_meta))]
    public partial class auth_form_mas
    {
        public string formid { get; set; }
        public string form_name { get; set; }
        public string model_id { get; set; }
    }
 
    [MetadataType(typeof(auth_form_mas_meta))]
    public partial class auth_form_mas_
    {
        public string formid { get; set; }
        public string form_name { get; set; }
        public string model_id { get; set; }
        public string model_name { get; set; }
    }


2. Linq Query > fill class list auth_form_mas_ with join linq query

        public async Task<ActionResult> Index()
        {
            var result = (from x in db.auth_form_mas
                          join y in db.auth_model_mas on x.model_id equals y.model_id
                          where 1==1
                          orderby x.form_name
                          select (new auth_form_mas_()
                          {
                              formid = x.formid,
                              form_name = x.form_name,
                              model_id = x.model_id,
                              model_name = y.model_name
                          })).ToList();
            List<auth_form_mas_> dl = result;
            return View(dl);
            //return View(await db.auth_form_mas.ToListAsync());
        }

3. Linq Query > fill class list auth_form_mas_ with left join

        public async Task<ActionResult> Index()
        {
            var result = (from x in db.auth_form_mas
                          join y in db.auth_model_mas on x.model_id equals y.model_id 
                          into ys from y in ys.DefaultIfEmpty()
                          where 1==1
                          orderby x.form_name
                          select (new auth_form_mas_()
                          {
                              formid = x.formid,
                              form_name = x.form_name,
                              model_id = x.model_id,
                              model_name = y.model_name
                          })).ToList();
            List<auth_form_mas_> dl = result;
            return View(dl);
        }


4. Another Example Linq Query > fill class list auth_form_mas_ with join linq query


            var data = from x in db.auth_profile
                       join p in db.auth_profile_master on x.profileid equals p.profileid
                       join f in db.auth_form_mas on x.formid equals f.formid
                       join u in db.auth_user on x.userid equals u.userid
                       where 1 == 1 && x.userid == d.userid
                       orderby f.form_name
                       select (new auth_profile_()
                       {
                           Srno = x.Srno,
                           userid = x.userid,
                           user_name = u.name,
                           username = u.username,
                           formid = x.formid,
                           form_name = f.form_name,
                           profileid = p.profile_name,
                           profile_name = p.profile_name,
                           add_ = x.add_,
                           view_ = x.view_,
                           edit_ = x.edit_,
                           del_ = x.del_,
                           Password = x.Password
                       });

            ViewBag.auth_profile_List = data.ToList();



5. Multiple Join in linq or Multi column join linq


var data = from x in db.v_Client
                           join y in db.City_Mas on x.city_code equals y.city_code
                           into ys
                           from y in ys.DefaultIfEmpty()
                           join z in db.State_Mas on x.state_code equals z.state_code
                           into zs
                           from z in zs.DefaultIfEmpty()
                           join t in db.Thana_Mas on x.th_code equals t.th_code
                           into ts
                           from t in ts.DefaultIfEmpty()
                           join c in db.Court_Mas on x.cou_code equals c.cou_code
                           into cs
                           from c in cs.DefaultIfEmpty()
                           join h in db.v_current_tbl_history on new { f1 = x.ad_code, f2 = x.cl_code } equals new { f1 = h.ad_code, f2 = h.cl_code }
                           into hs
                           from h in hs.DefaultIfEmpty()
                           join u in db.auth_user on x.cl_code equals u.userid
                           into us
                           from u in us.DefaultIfEmpty()

                           where (d.srno == 0 || d.srno == x.srno)
                           && (d.ad_code == null || d.ad_code == x.ad_code)
                           && (d.cl_code == null || d.cl_code == x.cl_code)
                           && (d.cl_name == null || x.cl_name.StartsWith(d.cl_name))
                           && (d.cou_code == null || d.cou_code == x.cou_code)
                           && (d.cou_name == null || c.cou_name.StartsWith(d.cou_name))
                           && (d.th_code == null || d.th_code == x.th_code)
                           && (d.th_name == null || t.th_name.StartsWith(d.cou_name))
                           && (d.city_code == null || x.city_code == d.city_code)
                           && (d.state_code == null || x.state_code == d.state_code)
                           && (d.city_name == null || y.city_name.StartsWith(d.city_name))
                           && (d.state_name == null || z.state_name.StartsWith(d.state_name))
                           && (d.skip_date == null || h.cl_nextdate < d.skip_date)
                           orderby x.cl_edate descending
                           select (new v_Client_()
                           {
                               srno = x.srno,
                               ad_code = x.ad_code,
                               cl_code = x.cl_code,
                               cl_id = x.cl_id,
                               cl_eno = x.cl_eno,
                               cl_edate = x.cl_edate,
                               cl_casetype = x.cl_casetype,
                               cl_name = x.cl_name,
                               cl_address = x.cl_address,
                               state_code = x.state_code,
                               city_code = x.city_code,
                               th_code = x.th_code,
                               cou_code = x.cou_code,
                               cl_contactno = x.cl_contactno,
                               cl_father = x.cl_father,
                               cl_sectionno = x.cl_sectionno,
                               cl_fees = x.cl_fees,
                               cl_paid = x.cl_paid,
                               cl_balance = x.cl_balance,
                               cl_assu1 = x.cl_assu1,
                               cl_assu2 = x.cl_assu2,
                               cl_assu3 = x.cl_assu3,
                               cl_assu4 = x.cl_assu4,
                               cl_assu5 = x.cl_assu5,
                               cl_assu1c = x.cl_assu1c,
                               cl_assu2c = x.cl_assu2c,
                               cl_assu3c = x.cl_assu3c,
                               cl_assu4c = x.cl_assu4c,
                               cl_assu5c = x.cl_assu5c,
                               cl_paricular = x.cl_paricular,
                               cl_client = x.cl_client,
                               cl_assu1add = x.cl_assu1add,
                               cl_assu1city = x.cl_assu1city,
                               cl_assu1fath = x.cl_assu1fath,
                               cnr_no = x.cnr_no,
                               parokar_name = x.parokar_name,
                               par_number = x.par_number,
                               par_address = x.par_address,
                               cl_status = x.cl_status,
                               edate = x.edate,
                               e_id = x.e_id,
                               udate = x.udate,
                               u_id = x.u_id,

                               city_name = y.city_name,
                               state_name = z.state_name,
                               cou_name = c.cou_name,
                               th_name = t.th_name,

                               hsrno = h.srno,
                               cl_nextdate = h.cl_nextdate,
                               cl_pastdate = h.cl_pastdate,
                               present_status = h.present_status,
                               future_status = h.future_status,
                               cl_remark = h.cl_remark,
                               jugment = h.jugment,

                               username = u.username,
                               passwd = u.passwd

                           });

                var dt = data.ToList();



No comments:

Post a Comment

Linq Expression syntax for where condtion in linq

(Expression<Func<T, bool>> filter)