I have an enumerable list of client and want to return a light weight "version" of my client object :
IEnumerable<Client> clientList = blablabla. (data is fetched here)
return clientList.Select(b=> new CustomerSearchResult { Number= b.AccountNumber, Name = b.FirstName+" " +b.LastName , ...});
Note that this is working and will a customerSearchResult object based on the ClientList
I want to create a FUNC (ex: CustomerSelector) to create a CustomerSearchResult with each client object (my goal is to avoid setting properties manually like this for each of my client lists) :
ex:
Expression<Func<Customer,CustomerSearchResult>> selector = (input) =>
new CustomerSearchResult
{
Number= b.AccountNumber,
Name = b.FirstName+" " +b.LastName ,
...
};
...
... //CustomerList is previously populated
return CustomerList.Select(selector);
I'm getting the following error : IEnumerable<Customer>
does not contain a definition for select
Is it possible to create a FUNC selector in this case ?
The problem is that you're using an expression tree - which is used by IQueryable<T>
(or rather, the extension methods in Queryable
targeting IQueryable<T>
). For Enumerable.Select
, you want a delegate instead. Just change your selector
declaration to:
Func<Customer, CustomerSearchResult> selector = input => { ... }
See more on this question at Stackoverflow