c# - Union on empty Enumerable -


i have function

public async task<iqueryable<document>> getdocuments(...) 

in search documents under given conditions. conditions can skipped. @ end perform union of these queries.

   var documents = await documentservice.getdocuments(this, userid,                 roleshowfullnumber, param.ordercolname(), param.searchvalue, filter);   var usersgroupsid = filter.usersgroupsid;         if (usersgroupsid != null)         {             if (!usersgroupsid.contains("all"))             {                 iqueryable<document> mydocs = enumerable.empty<document>().asqueryable();                 if (usersgroupsid.contains("myorders"))                 {                     mydocs = documents.where(x => x.ownerid == userid || x.userid == userid);                     usersgroupsid = usersgroupsid.where(x => x != "myorders").toarray();                 }                 iqueryable<document> wards = enumerable.empty<document>().asqueryable();                 if (usersgroupsid.contains("wards"))                 {                     var relateduserid = _db.users.where(x => x.id == userid).select(x => x.relateduserid).firstordefault();                     if (relateduserid != null)                     {                         var mywards = _db.kh__kontrahent.where(x => x.kh_idopiekun == relateduserid);                         var mywardsusers = _db.users.where(x => mywards.any(w => w.kh_id == (x.relatedcustomerid == null ? -1 : x.relatedcustomerid)));                         wards = documents.where(x => (mywardsusers.any(w => x.userid == w.id) || mywardsusers.any(w => x.ownerid == w.id)));                         usersgroupsid = usersgroupsid.where(x => x != "wards").toarray();                     }                 }                  iqueryable<document> groups = enumerable.empty<document>().asqueryable();                 if (usersgroupsid.length > 0)                 {                     var usersgroups = _db.groups.where(x => usersgroupsid.contains(x.id.tostring()));                     var userslist = usersgroups.select(x => x.users);                     var users = userslist.selectmany(x => x);                     var usersid = users.select(x => x.id);                     groups = _db.documents.where(x => (usersid.any(u => u == x.ownerid) || usersid.any(u => u == x.userid)));                 }                  documents = mydocs.union(wards).union(groups);              }         } 

but if 1 of these partial queries empty (was skipped) when try obtain these documents in way shown below got error.

   var documentspaginated = await documents.skip(param.start)                                     .take(param.length)                                     .tolistasync(); 

error: source iqueryable doesn't implement idbasyncenumerable.

how can make function able skip sub queries , union all. prefer not change function return value.

try this, althought code seems have massive amount of code smell...

public async task<iqueryable<document>> getdocuments(...)          var documents = await documentservice.getdocuments(this, userid,                 roleshowfullnumber, param.ordercolname(), param.searchvalue, filter);   var usersgroupsid = filter.usersgroupsid;         if (usersgroupsid != null)         {             if (!usersgroupsid.contains("all"))             {                 iqueryable<document> mydocs = null;                 if (usersgroupsid.contains("myorders"))                 {                     mydocs = documents.where(x => x.ownerid == userid || x.userid == userid);                     usersgroupsid = usersgroupsid.where(x => x != "myorders").toarray();                 }                 iqueryable<document> wards = null;                 if (usersgroupsid.contains("wards"))                 {                     var relateduserid = _db.users.where(x => x.id == userid).select(x => x.relateduserid).firstordefault();                     if (relateduserid != null)                     {                         var mywards = _db.kh__kontrahent.where(x => x.kh_idopiekun == relateduserid);                         var mywardsusers = _db.users.where(x => mywards.any(w => w.kh_id == (x.relatedcustomerid == null ? -1 : x.relatedcustomerid)));                         wards = documents.where(x => (mywardsusers.any(w => x.userid == w.id) || mywardsusers.any(w => x.ownerid == w.id)));                         usersgroupsid = usersgroupsid.where(x => x != "wards").toarray();                     }                 }                  iqueryable<document> groups = null;                 if (usersgroupsid.length > 0)                 {                     var usersgroups = _db.groups.where(x => usersgroupsid.contains(x.id.tostring()));                     var userslist = usersgroups.select(x => x.users);                     var users = userslist.selectmany(x => x);                     var usersid = users.select(x => x.id);                     groups = _db.documents.where(x => (usersid.any(u => u == x.ownerid) || usersid.any(u => u == x.userid)));                 }                 if(mydocs != null)                     documents = documents.union(mydocs);                 if(wards != null)                     documents = documents.union(wards);                 if(groups != null)                     documents = documents.union(groups);              }         } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -