c# - Advanced Search using Lambda Expression -
i want implement advanced search in asp.net mvc application, user can select 1 or more criteria product search.
let's have these criterias: color, size, price range.
this far till now
productsizelist = db.productsizes.where(productsize => (string.isnullorempty(productcolorid) || productsize.product.productcolors.where(a => a.colorid == intcolorid).any()) ).groupby(x => x.productid).select(grouped => grouped.firstordefault()).tolist();
i have many-to-many relationship between product
, color
tables, , productcolor
1 linking them. same thing product
, productsize
, size
tables.
the code working size
, , price range
. problem color
, because .any()
returns when finds first product
. if there more 1 product
, returns first one.
so, want know if there method, or way able products specified color.
i searched lot, , know can build clause dynamically, think work requirements. if there simple fix more happy.
edit
i removed working-as-wanted code suggested @jeroen, , left 1 want fix.
solved
i fixed it, combining @jason , @marlon answers. i'll put solution in separate answer. want understand 2 points:
- why worked correctly when based query on product?
- why `.distinct()` didn't thing, , got duplicated products.
please try this. uses iqueryable let more construct conditions before executing against database when tolist
called.
var query = db.productsizes.asqueryable(); if (string.isnullorempty(productcolorid) == false) query = query.where(productsize => productsize.product.productcolors.any(a => a.colorid == intcolorid)) if (string.isnullorempty(sizeid) == false) query = query.where(productsize => productsize.size.id == intsizeid); if (string.isnullorempty(from) == false) query = query.where(productsize => productsize.price >= decimalfrom); if (string.isnullorempty(to) == false) query = query.where(productsize => productsize.price <= decimalto); var productsizelist = query .select(productsize => productsize.productid) .distinct() .tolist();
Comments
Post a Comment