Let’s say I have this query I pass to a repository:
var results = userRepository.Get(u => u.Username == "JDoe" && u.Password == "123456");
Now, let’s say I create an expression to format the results a certain way:
Expression<Func<User,string>> userDisplay = u => u.Firstname + " " + u.LastName + " - " + u.CompanyName
So I may have to write my own extension, but something like this:
var formatedResults = results.Format(userDisplay);
Update:
What about a more complicated solution to project the results into another object:
public class SearchResult
{
object EntityId {get; set;}
object Displaytext {get; set;}
}
So, using the same idea to use the specific display expression, what is a good way to project the results into the SearchResult object?
you should be able to just call
Update As noted in the comments Select doesn’t accept an Expression argument. Unless
userDisplayneeds to be an expression it could just be updated as a delegate:Update
Select allows you to transform whatever you are iterating over.
Some examples of what you could do: