Is it possible to create a custom lambda function that I can replace with the .Contains()/.StartsWith()/EndsWith() calls below?
If so, I don’t have to compare the search string here, but I can do it in this custom function. This would remove 2/3 of the code below, if I’m right.
…or if you have any other ideas on how to minimize this I’d be glad to hear it!
private void searcher(ref Expression<Func<Party, bool>> predicate, string search, string keyword, string column)
{
if (search == "contain")
{
if (column == "surname") predicate = predicate.And(p => p.surname.Contains(keyword));
if (column == "lastname") predicate = predicate.And(p => p.lastname.Contains(keyword));
if (column == "comment") predicate = predicate.And(p => p.comment.Contains(keyword));
if (column == "position") predicate = predicate.And(p => p.position.Contains(keyword));
}
else if (search == "start")
{
if (column == "surname") predicate = predicate.And(p => p.surname.StartsWith(keyword));
if (column == "lastname") predicate = predicate.And(p => p.lastname.StartsWith(keyword));
if (column == "comment") predicate = predicate.And(p => p.comment.StartsWith(keyword));
if (column == "position") predicate = predicate.And(p => p.position.StartsWith(keyword));
}
else if (search == "end")
{
if (column == "surname") predicate = predicate.And(p => p.surname.EndsWith(keyword));
if (column == "lastname") predicate = predicate.And(p => p.lastname.EndsWith(keyword));
if (column == "comment") predicate = predicate.And(p => p.comment.EndsWith(keyword));
if (column == "position") predicate = predicate.And(p => p.position.EndsWith(keyword));
}
}
Might be tempted to write an extension for string (Note this would be better still with an enum for
where):Then your code becomes a bit simpler: