Hide Join if string.IsNullOrEmpty using Linq -


i got linq query searches selected values in database using dropdowns.

is there way hide "join" in linq query if ddlcategory null? want because result of search shows duplicated-rows because documents can have many categories.?? hope understand mean.. can help??

var documents = d in data.tbldocuments              join sc in data.tblsubcategories on d.docid equals sc.docid             orderby d.docyear descending                               (string.isnullorempty(person) || d.docperson.equals(person)) &&                 (string.isnullorempty(year) || d.docyear.equals(year)) &&                 (string.isnullorempty(law) || d.doclaw.equals(law)) &&                 (string.isnullorempty(court) || d.doccourt.equals(court)) &&                 (string.isnullorempty(category) || sc.categoryid.equals(category)) &&                 (string.isnullorempty(casenr) || d.docnr.equals(casenr))              select d; 

use lambda syntax:

var query = data.tbldocuments;  if (condition) // conditionally add join    query = query.join(data.tblsubcategories.where(sc => sc.categoryid == category),                        d => d.docid, sc => sc.docid, (d,sc) => d);  // continue compose query query = query.orderbydescending(d => d.docyear)              .where(d => ...); 

btw can compose filtering based on conditions:

if (!string.isnullorempty(person))     query = query.where(d => d.docperson == person); 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -