c# - LINQ select: want to pass collection into CONTAINS -
the page has 6 drop-down lists , user can select single item or leave list on first item 'any'. in case, want return all. instance, if user chooses soil drop-down ... choices any, sand, loam , clay. if choose any, want select soil types soil table. how pass collection of soil types contains? like:
*where soil.soil1.contains("sand", "loam", "clay")* public list<plantlist> getmultiselectplantsearchresultbesomolihezo(string benefit, string soil, string moisture, string light, string height, string zone) { var plantlist = plant in db.plants join benefit in db.benefits on plant.plant_id equals benefit.plant_id join soil in db.soils on plant.plant_id equals soil.plant_id join moisture in db.moistures on plant.plant_id equals moisture.plant_id join light in db.lights on plant.plant_id equals light.plant_id join height in db.heights on plant.plant_id equals height.plant_id join zone in db.zones on plant.plant_id equals zone.plant_id plant.plant_id == benefit.plant_id && benefit.benefit1 == benefit && soil.soil1 == soil && moisture.moisture1 == moisture && light.light1 == light && height.height1 == height && zone.zone1 == convert.toint16(zone) select new plantlist { plantid = plant.plant_id, plantname = plant.pl_name, plantgenus = plant.pl_genus, plantspecies = plant.pl_species, plantdesc = plant.pl_desc, plantimagethumb = plant.pl_image_thumb, plantbenefit = benefit.benefit1, plantsoil = soil.soil1, plantmoisture = moisture.moisture1, plantlight = light.light1, plantheight = height.height1, plantzone = zone.zone1 }; var distinctitems = plantlist.orderby(z => z.plantname).groupby(x => x.plantid).select(y => y.firstordefault()).tolist(); return distinctitems;
you can use composability of iqueryable
:
iqueryable<soil> soils = db.soils; if (soiltypes.any()) { soils = soils.where(s => soiltypes.contains(s.type)); } var plantlist = plant in db.plants join benefit in db.benefits on plant.plant_id equals benefit.plant_id join soil in soils on plant.plant_id equals soil.plant_id ...
soiltypes
contains selection of types user made (1 or more types list of strings). if user chose "any" should empty list (which means selection criterion ignored). should compose soiltypes
based on user selected in ui.
this translated 1 sql query, soils
expression blended plantlist
query.
Comments
Post a Comment