c# - Filtering a string array when elements represented (partially) in an other array of strings -


i'm reconstructing following statement.

ienumerable<string>    input = ...,   filter = ...,   output = input.where(filter.contains(element)); 

for now, works supposed words matched way need exact. in language of customer there lot of conjugations , requirement posted use joker characters ("dog" should match "dog", "doggy" , "dogmatic").

i've suggested following change. sure, though, if can regarded smooth eyes. can suggest improvement or gets?

ienumerable<string>    input = ...,   filter = ...,   output = input.where(word => filter.any(head => word.startswith(head))) 

i considering iequalitycomparer implementation that's objects of same type, while condition on string contra ienumerable.

generally, have linq statement fine , don't see big issue being "smooth on eyes" (linq calls can more out of hand this).

if want, move filter.any(head => word.startswith(head)) separate func<string, bool> delegate , pass in:

func<string, bool> myconstraint = word => filter.any(head => word.startswith(head));  output = input.where(myconstraint); 

you can move constraint construction separate method may open door flexibility client if matching rules change or have cover more complicated cases:

private func<string, bool> buildconstraints() {     filter = ...,     if (checkequalityonly)         return word => filter.contains(word);     else         return word => filter.any(head => word.startswith(head));  }  output = input.where(buildconstraints()); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -