Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select<TKey>(Expression<Func<T, TKey>> fields) method question #52

Closed
CaspianCanuck opened this issue Nov 27, 2013 · 3 comments
Closed

Comments

@CaspianCanuck
Copy link
Contributor

Hi,

This is more of a question than an issue report:

Method Select<TKey>(Expression<Func<T, TKey>> fields) returns SqlExpression<T> but shouldn't it return SqlExpression<TKey> instead?

I.e. if I am doing a projection like .Select(x => new { ID = x.ID, Name = x.Name }) then the result should be an expression of the anonymous type TKey, right? At least that's how it works in Linq.

Can you comment on that please?

P.S. Just as a background: I am working on a common repository interface that can support both NPoco- and EF-based concrete repositories, and this is one of the requirements.

@schotime
Copy link
Owner

Projections in NPoco are implemented to only select certain columns for an entity.

eg.

public class User {
    public int Id {get;set;}
    public string Name {get;set;}
    public byte[] Avatar {get;set;}
}

For instance, if I had the above User but I didn't want to select the Avatar this time, I could run.

db.FetchBy<User>(sql => sql.Select(x=> new { x.Id, x.Name }))

This is still based on the User object, so that if I then needed a where I could.

db.FetchBy<User>(sql => sql.Select(x=> new { x.Id, x.Name }).Where(x=>x.Avatar == null)

If it returned SqlExpression<TKey> the above statement would not be able to access the avatar property.

Does this make sense?

@CaspianCanuck
Copy link
Contributor Author

Yep, makes sense. What I've done in my repository class is add the
Linq-to-Objects Select() projection on the enumerated results of the query,
e.g.:

db.FetchBy(sql => sql.Select(x=> new { x.Id, x.Name }).Where(x
=> x.Id == 1).Select(x => new { x.Id, x.Name });

This gives me precisely what I want -- a Linq-like behaviour (so I can keep
my NPoco repo's interface consistent with that of my EF repo), albeit at
the expense of a few extra CPU cycles.

On Wed, Nov 27, 2013 at 7:19 PM, Adam Schroder notifications@github.comwrote:

Projections in NPoco are implemented to only select certain columns for an
entity.

eg.

public class User {
public int Id {get;set;}
public string Name {get;set;}
public byte[] Avatar {get;set;}}

For instance, if I had the above User but I didn't want to select the
Avatar this time, I could run.

db.FetchBy(sql => sql.Select(x=> new { x.Id, x.Name }))

This is still based on the User object, so that if I then needed a where I
could.

db.FetchBy(sql => sql.Select(x=> new { x.Id, x.Name }).Where(x=>x.Avatar == null)

If it returned SqlExpression the above statement would not be able
to access the avatar property.

Does this make sense?


Reply to this email directly or view it on GitHubhttps://github.com//issues/52#issuecomment-29431089
.

Tim

P.S. If you have trouble reaching me at tim@askerov.net
tim@askerov.net
please
try taskerov@gmail.com taskerov@gmail.com instead.

@schotime
Copy link
Owner

Cool.
That shouldn't cause too much overhead.
Adam

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants