LINQ: DefaultIfEmpty() causes slow response

154 Views Asked by At

I'm facing an issue where DefaultIfEmpty() causes slow response. The code is like:

rslt = (
    from p in P.Where(p => p.fcode == fCode && p.year == year && p.is_deleted == false && p.pname != pNameS)
    from c in C.Where(c => c.fcode == fCode && c.ccode == p.ccode)
    from pf in PF.Where(pf => pf.fcode == fCode && pf.pcode == p.pcode).DefaultIfEmpty()
    from defHU in DEF.Where(def => def.fixed_key == Constants.HU && pf.hu == def.def_key).DefaultIfEmpty()
    from defAU in DEF.Where(def => def.fixed_key == Constants.AU && pf.pau == def.def_key).DefaultIfEmpty()
    select new PPShow{
        Year = p.year,
        PCode = p.pcode,
        PName = p.production_name,
        FCode = pf.fcode,
        Br = p.br,
        CName = c.cname,
        PArea = pf.parea,
        PAU = pf.pau,
        PAUName = defAU.def_data,
        HA = pf.ha,
        HUName = defHU.def_data,
        SDate = p.sdate,
        PDate = p.pdate,
        StartDate = p.start_date,
        EndDate = p. end_date,
        Flag = 1
    }).ToList();

I have tried to query for 10,000 records and it took approximately 8.5s to finish, while it took only about 1s if I remove all the DefaultIfEmpty().

I wonder what DefaultIfEmpty() actually does. Also, is there any solution to my case?

0

There are 0 best solutions below