I have a multiselection box and I would like to iterate over the selected items and do a linq query, but I'm not sure how to write it. This is what I have so far:
if (lbStateLegislation.Items.Count > 0)
{
foreach (ListItem li in lbStateLegislation.Items)
attributes = vdc.attributes.Where(a => a.fieldvalue == li.Value).ToList();
}
I basically need to construct an OR query, so it is selecting from the collection where there are values for each of the selected items. I think, as it's written now, it is doing an AND query.
Just use the Contains extension method. Linq2Sql will translate it as an IN clause:
var inValues = lbStateLegislation.Items.Select(s => s.Value);
vdc.attributes.Where(a => inValues.Contains(a.fieldvalue));
You may be able to combine the two statements into one, but I will leave that for you to try as I'm not positive that it will work as a single statement.
HTH
var attributes=
vdc.attributes.Where(q=>
lbStateLegislation.Items.Any(o=> o.Value == q.fieldValue))
.Select(o=> o);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.