Include where clause on linq query when param is not null Npgsql Include where clause on linq query when param is not null Npgsql postgresql postgresql

Include where clause on linq query when param is not null Npgsql


I currently have the same cases. I think the problem is the lack of recognition, by NpgSQL, of string.IsNullOrEmpty.

I replaced the test with a check on empty string, always recognizing as not NULL the input parameter.

-- bad

        var data = from art in _ctx.Set<Soleo.Model.DLAR>()                   from iva in _ctx.Set<Soleo.Model.DLAI>().Where(k => k.DITTA == art.DITTA && k.COD == art.CIVA).DefaultIfEmpty()                   from fam in _ctx.Set<Soleo.Model.DLFA>().Where(k => k.DITTA == art.DITTA && k.COD == art.FAM).DefaultIfEmpty()                   from mar in _ctx.Set<Soleo.Model.DLMA>().Where(k => k.DITTA == art.DITTA && k.COD == art.MAR).DefaultIfEmpty()                   from udm in _ctx.Set<Soleo.Model.DLUM>().Where(k => k.DITTA == art.DITTA && k.COD == art.UM).DefaultIfEmpty()                   where art.DITTA == DLAUTH.Config.Current.DITTA && art.COD.Contains(sel_cod) && art.DES.Contains(sel_des)                   && (string.IsNullOrEmpty(sel_fam) || string.Compare(art.FAM, sel_fam, true) == 0)                   && (string.IsNullOrEmpty(sel_mar) || string.Compare(art.MAR, sel_mar, true) == 0)                   && (art.DIS >= sel_dis_da && art.DIS <= sel_dis_a)                   select new                   {                       COD = art.COD,                       DES = art.DES,                       DES_UDM = udm.DES,                       DES_MAR = mar.DES,                       DES_FAM = fam.DES,                       DES_CIVA = iva.DES,                       MAG1 = art.MAG1,                       MAG2 = art.MAG2,                       DES_DIS = art.DIS == 1 ? "Si" : "No"                   };

-- good:

            var data = from art in _ctx.Set<Soleo.Model.DLAR>()                   from iva in _ctx.Set<Soleo.Model.DLAI>().Where(k => k.DITTA == art.DITTA && k.COD == art.CIVA).DefaultIfEmpty()                   from fam in _ctx.Set<Soleo.Model.DLFA>().Where(k => k.DITTA == art.DITTA && k.COD == art.FAM).DefaultIfEmpty()                   from mar in _ctx.Set<Soleo.Model.DLMA>().Where(k => k.DITTA == art.DITTA && k.COD == art.MAR).DefaultIfEmpty()                   from udm in _ctx.Set<Soleo.Model.DLUM>().Where(k => k.DITTA == art.DITTA && k.COD == art.UM).DefaultIfEmpty()                   where art.DITTA == DLAUTH.Config.Current.DITTA && art.COD.Contains(sel_cod) && art.DES.Contains(sel_des)                   && (string.Compare(sel_fam, "", true) == 0 || string.Compare(art.FAM, sel_fam, true) == 0)                   && (string.Compare(sel_mar, "", true) == 0 || string.Compare(art.MAR, sel_mar, true) == 0)                   && (art.DIS >= sel_dis_da && art.DIS <= sel_dis_a)                   select new                   {                       COD = art.COD,                       DES = art.DES,                       DES_UDM = udm.DES,                       DES_MAR = mar.DES,                       DES_FAM = fam.DES,                       DES_CIVA = iva.DES,                       MAG1 = art.MAG1,                       MAG2 = art.MAG2,                       DES_DIS = art.DIS == 1 ? "Si" : "No"                   };

But I do not think this is the solution. I will report the case to NpgSQL.