c# - Linq Query with Foreach -
i have collection of policies contain collection of summaries contain servicename , product type can have comma separated products. need find if of policy summary servicenames matches of comma separated values in product type. example:
producttype.split(',') .select(p => p.equals(policies.summaries.foreach( s => { s.servicename = p})));
and
var name = s in producttype.split(',') s = policies.summaries.foreach(p=> { p.servicename == s}) select s;
i know above wont compile wondered if can done in linq
yes possible, seek that:
var producttypes = producttype.split(','); //if need matched policies var matchedpolicies = policies .where(x => x.summaries.any(y => producttypes.contains(y.servicename))); //if need matched summaries var matchedsummaries = policies.selectmany(x => x.summaries) .where(x => producttypes.contains(x.servicename));
and can utilize matchedpolicies.any()
or matchedsummaries.any()
determine if of policy summary servicenames matches of comma separated values in product type.
alternatively if don't care concrete matched policies can utilize any
right away policies.any(x => x.summaries.any(y => producttypes.contains(y.servicename)))
also suggest 101 linq samples additional reading great examples.
c# linq foreach
No comments:
Post a Comment