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

How to apply projections (Select) in the latest version of the framework? #434

Open
rbravo-msw opened this issue Feb 3, 2025 · 2 comments

Comments

@rbravo-msw
Copy link

Hi,

I’ve been using Ardalis.Specification in my project, and after updating to the latest version, I noticed that the Select() function has been removed. This prevents defining projections directly within specifications.

Previously, I used Select() inside a specification to return only the fields I actually needed, avoiding the need to load entire entities from the database. Without this function, it seems that I now have to fetch all properties of an entity, which could impact performance in large queries.

My questions are:

What is the recommended approach for handling projections in the latest version of the framework?
Is there any built-in alternative to avoid loading all entity properties when only a subset of fields is needed?
Are there any plans to reintroduce Select() or a similar feature in future versions?

Any guidance on how to address this issue would be greatly appreciated.
Thanks for your work on this project!

@fiseni
Copy link
Collaborator

fiseni commented Feb 3, 2025

Hi @rbravo-msw,

The projection feature is not removed. It's always been there and we don't intend to remove it.
There's a caveat though, you won't be able to place it at the end of the query. You must have an additional Query statement as follows.

class Customer
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

class CustomerSpec : Specification<Customer, string>
{
    public CustomerSpec(int id)
    {
        Query.Where(c => c.Id == id);

        Query.Select(c => c.FirstName);
    }
}

In version 9 we plan to improve the builders, and you'll be able to write everything in a single statement with Select at the end.

@ngoquoctoandev
Copy link

@fiseni This is great information, thank you so much, long time no update, I am eagerly waiting for version 9.0

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

3 participants