views:

67

answers:

2

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 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..).

+2  A: 

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 }; 
Adam Robinson
See my edit above. I know that what you posted is possible. It's just not what I'm trying to do.
TheCloudlessSky
A: 

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).

Jon Skeet
@Jon - See my edit for what I'm trying to do.
TheCloudlessSky
@TheCloudlessSky: I'm not entirely convinced I understand, but I've edited my answer with another idea.
Jon Skeet
Yeah that's what I was planning to do. Now this *works* but when I project using an Entity and NOT a POCO I get a `NotSupportedException` with "The entity or complex type 'Data.Repositories.User' cannot be constructed in a LINQ to Entities query." as the message :(. If I use a POCO, it works just fine. I don't really want to have to maintain *two* classes of Users... that's kind of lame. I was thinking of just using `object` and then using the DLINQ library to build the string...
TheCloudlessSky
See this post for what I mean: http://stackoverflow.com/questions/1338362/is-it-possible-to-use-selectl-new-with-selectmany-in-entityframework/1340976#1340976If Microsoft would hurry up, I could be using the Code-Only support. I just don't know how to map many-to-many with it currently...
TheCloudlessSky
@TheCloudlessSky: I think we're now going somewhat beyond the original question, to be honest...
Jon Skeet
Of course I was, I was mostly just talking out loud :) ... sorry! Anyways, thanks for your answer.
TheCloudlessSky