This might be a badly worded question for what I’m trying to learn so I’ll explain what I am trying to do. I have the following list being created using the following expression:
var showIndexes = from item in lItems
where WildcardString.IsMatch(item.RecordId.ToString(), filter) ||
WildcardString.IsMatch(item.Tokens.ToString(), filter) ||
WildcardString.IsMatch(string.Format("{0}:{1}", item.OwnerID.SystemName, item.OwnerID.Port.ToString()), filter) ||
WildcardString.IsMatch(item.ChildID.UserName, filter) ||
WildcardString.IsMatch(item.TypeOfLicense.ToString(), filter) ||
WildcardString.IsMatch(item.ExpiryTime.DateTime.ToLocalTime().ToString(), filter)
select item.RecordId.ToString();
I want to put this expression into a function, and pass in constrainments used in the “where” in as a single parameter. How do I achieve this? I’ve seen similar functionality using the bitwise OR when working with file attributes (when you select different attribute enums) so I believe it should be possible to write my own function which takes a group of delegates as the “where” rule.
If it is relevant, WildcardString.IsMatch(string, string) is a boolean function. Also, I have three different functions performing the exact same thing but on different lists, which is why I’d like to learn how to do reduce this to a single function, and just pass the list and the selection rules in.
Simply define a predicate: a function that takes an
itemand returns a boolean.Assuming that
itemis of typeRecordand thatfilteris in scope:You can then treat
predicateas a first-class value (store it, pass it as a parameter, etc) and filter anyIEnumerable<Record>with