I have LINQ statement that looks like this:
return ( from c in customers select new ClientEntity() { Name = c.Name, ... });
I’d like to be able to abstract out the select into its own method so that I can have different ‘mapping’ option. What does my method need to return?
In essence, I’d like my LINQ query to look like this:
return ( from c in customers select new Mapper(c));
Edit:
This is for LINQ to SQL.
New answer now I’ve noticed that it’s Linq to SQL… 🙂
If you look at the version of Select that works on
IQueryable<T>it doesn’t take aFunc<In, Out>. Instead, it takes anExpression<Func<In, Out>>. The compiler knows how to generate such a thing from a lambda, which is why your normal code compiles.So to have a variety of select mapping functions ready to use by passing them to Select, you could declared them like this:
You would then use them like this: