I'm using LINQ-to-Entities. Using the following query:
var x = from u in context.Users select new { u.Id, u.Name };
That only selects the Id
and Name
columns. Great.
I'm trying to make a repository that can be passed that new { u.Id, u.Name}
as a parameter to allow the client to pick which columns are used in the SELECT statement.
I'm trying to use the "fluent" interface with my repository instead of the query syntax. Basically the final call is to convert IQueryable to IList and actually execute the code. I've looked at the DynamicExpressions library but I'm not really sure this is the route I want to take for this. It's possible to do it with the query syntax, but not the fluent interface?
Edit: Sorry I should have mentioned that all queries are encapsulated inside the repository. So for example I want to be able to expose a method like the following:
public void Project(Expression<Func<TEntity, TEntity>> fields)
{
this.Projection = fields;
}
So calling this would be like:
using (DBContext context = new DBContext())
{
IUserRepository repo = new UserRepository(context);
repo.Project(u => new { u.Id, u.Name });
repo.GetById(100);
}
Inside of GetById
would be something like ctx.Users.Where(u => u.Id == id).Select(this.Projection)
.
This way, which columns are returned can be selected by the calling code. The reason for this is because maybe I want to return a User object but maybe I only need the Id and Name (and thus returning less data over the wire).
The problem is that I obviously can't convert the anonymous type to User. It would be cool if I could do something like:
repo.Project(u => new User() { Id = u.Id, Name = u.Name });
Which would mean I don't need to create an anonymous on type. EDIT: Ok this seems to work ONLY if the type that's returned is a POCO ... ugh.
EDIT2: I开发者_JAVA百科 might have to go the DLINQ approach. What I'm thinking is having Expression<Func<TEntity, object>>
(to use the anonymous type) and then use reflection to get the list of properties. Then, using DLINQ build a string expression. The only downside is that I don't really want to use DLINQ as it adds a little overhead (Reflection.Emit etc..).
I'm afraid I'm not quite sure what you're actually asking, but that query is easy to write using dot notation (the term I use for what I think you call fluent interface):
var x = context.Users.Select(u => new { u.Id, u.Name });
As Adam says, if you're trying to create something which takes this tuple as a value in a strongly typed way, you're going to need a named type. You can either create your own, if you're using .NET 4 you could use Tuple<T1, T2>
.
That doesn't seem to fit with the bit of your question about claiming you can do what you want with a query expression though... could you elaborate?
EDIT: Okay, now I've got some idea of what you're talking about...
... you should make the Project
method take an expression tree of type Expression<Func<TInput, TOutput>>
and return something which uses TOutput
- e.g. a RepositoryProjection<TOutput>
. That way the anonymous type is still effectively captured within your code - so you can use it later (e.g. to put an extra condition on the query).
Using the syntax of new
without a type name (as you're doing) creates an anonymous type. While these are useful, anonymous types cannot be exposed outside of the function that defines them. If you need to be able to pass that specific entity to another function, you'll have to define an actual class with those properties somewhere in your code and instantiate it as part of the query. For example:
public class MyUser
{
public int Id { get; set; }
public string Name { get; set; }
}
Then:
var x = from u in context.Users select new MyUser { Id = u.Id, Name = u.Name };
精彩评论