将参数传递给linq查询

bxgwgixi  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(184)

下面的代码可以很好地工作。

public Task<List<RsvItemsViewModel>> GetReservations(int company_ID)
 {
            try
            {

                // we cannot pass parameter as company_id . we use hard coded values as 1 for CompanyId.Must find a way to overcome this
                return ((from a in gTravelDbContext.Set<Frsvitem>()
                            join d in gTravelDbContext.Set<Freserv>()
                                on a.Rsinum equals d.Rsvnum into gd from d in gd.Where(p1 => p1.CompanyId == 1)

                            join c in gTravelDbContext.Set<Fsupplier>()
                                on a.SupplId equals c.SupplId into gc from c in gc.Where(p1=>p1.CompanyId == 1)
                            join i in gTravelDbContext.Set<Fzone>()
                                on a.Rsizone equals i.Zoneid into gi
                            from i in gi.DefaultIfEmpty() where i.CompanyId == 1
                            
                            join g in gTravelDbContext.Set<Fservtype>()
                                on a.Rsisertype equals g.Stypecode
                            
                            join b in gTravelDbContext.Set<Fcustomer>()
                                 on d.CustId equals b.CustId
                            join e in gTravelDbContext.Set<Fpaykind>()
                                  on d.Rsvpaymethod equals e.Payid into ge
                            from e in ge.DefaultIfEmpty()
                            join f in gTravelDbContext.Set<Fsalesman>()
                                  on d.Rsvsalescode equals f.Salescode into gf
                            from f in gf.DefaultIfEmpty()
                            join h in gTravelDbContext.Set<Fpinake>()
                                  on d.Rsvakirosi equals h.Tblid into gh
                            from h in gh.Where(p => p.Tblcd == "yesno")
                            select new RsvItemsViewModel
                            {
                                Rsinum = a.Rsinum,
                                Stypename = g.Stypename,
                                Cuname = b.Cuname,
                                Rsvcuname = d.Rsvcuname,
                                Sumame = c.Suname,
                                Rsvakirosi = d.Rsvakirosi,
                                Yesno = h.Tbltext ,
                                Paytext = e.Paytext,
                                Salesname = f.Salesname,
                                Stypegroup = g.Stypegroup,
                                Company_id =d.CompanyId.GetValueOrDefault(),
                                Zonename = i.Zonename,
                                Rsisertype = a.Rsisertype,
                                Suppl_id = a.SupplId,
                                Xrhsh = d.Xrhsh,
                                Bpar_id = a.BparId

                            }
                             
                            
                            ).ToListAsync());

              

              

            }
            catch (Exception)
            {

                throw;
            }
 }

问题是,我想在linq中将company_ID作为参数传递,并将Where(p1 =〉p1.CompanyId == 1)替换为Where(p1 =〉p1.CompanyId == company_ID)
如果有人能帮助我,我将不胜感激。
谢谢你

olhwl3o2

olhwl3o21#

您以某种方式使用了GroupJoin,EF Core不支持这种方式。GroupJoin有很多限制,只有在需要LEFT JOIN时才使用它。
我已经重写了您的查询以便可以翻译:

var query = 
    from a in gTravelDbContext.Set<Frsvitem>()
    from d in gTravelDbContext.Set<Freserv>().Where(d => d.Rsvnum == a.Rsinum && d.CompanyId == company_ID)
    from c in gTravelDbContext.Set<Fsupplier>().Where(c => a.SupplId == c.SupplId && c.CompanyId == company_ID)
    from i in gTravelDbContext.Set<Fzone>().Where(i => a.Rsizone == i.Zoneid && c.CompanyId == company_ID).DefaultIfEmpty()
    
    join g in gTravelDbContext.Set<Fservtype>()
        on a.Rsisertype equals g.Stypecode
    
    join b in gTravelDbContext.Set<Fcustomer>()
            on d.CustId equals b.CustId

    join e in gTravelDbContext.Set<Fpaykind>()
            on d.Rsvpaymethod equals e.Payid into ge
    from e in ge.DefaultIfEmpty()

    join f in gTravelDbContext.Set<Fsalesman>()
            on d.Rsvsalescode equals f.Salescode into gf
    from f in gf.DefaultIfEmpty()

    from h in gTravelDbContext.Set<Fpinake>().Where(h => d.Rsvakirosi == h.Tblid && h.Tblcd == "yesno")
    select new RsvItemsViewModel
    {
        Rsinum = a.Rsinum,
        Stypename = g.Stypename,
        Cuname = b.Cuname,
        Rsvcuname = d.Rsvcuname,
        Sumame = c.Suname,
        Rsvakirosi = d.Rsvakirosi,
        Yesno = h.Tbltext ,
        Paytext = e.Paytext,
        Salesname = f.Salesname,
        Stypegroup = g.Stypegroup,
        Company_id =d.CompanyId.GetValueOrDefault(),
        Zonename = i.Zonename,
        Rsisertype = a.Rsisertype,
        Suppl_id = a.SupplId,
        Xrhsh = d.Xrhsh,
        Bpar_id = a.BparId
    }

相关问题