Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 587327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:12:03+00:00 2026-05-13T15:12:03+00:00

suppose in an entity there are attributes id, username, age, address. Now I just

  • 0

suppose in an entity there are attributes id, username, age, address. Now I just want id and username and I use this code for it.

Projections enable the returning of something other than a list of entities from a query.

var proj = Projections.ProjectionList()
    .Add(Projections.Property("Id"), "Id")
    .Add(Projections.Property("Username"), "Username");

var list2 = DetachedCriteria.For<User>()
    .Add(Expression.Eq("Username", "lachlan"))
    .GetExecutableCriteria( sessionFactory.GetCurrentSession())
    .SetProjection( proj )
    .List();

How will I retrieve the values. In which object will these value be taken.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:12:03+00:00Added an answer on May 13, 2026 at 3:12 pm

    Unless a result transformer is used, a projection will result in a list of anonymous objects with the projected values. This would be sufficient for databinding.

    For other uses, you want to set a result transformer which will create objects of a known type. The AliasToBeanTransformer will create an object of the specified type for each row, and set its properties to the row values.

    If you know the type of the results, you can use the generic List<T>() method.

    var proj = Projections.ProjectionList()
        .Add(Projections.Property("Id"), "Id")
        .Add(Projections.Property("Username"), "Username");
    
    var list2 = DetachedCriteria.For<User>()
        .Add(Expression.Eq("Username", "lachlan"))
        .GetExecutableCriteria( sessionFactory.GetCurrentSession())
        .SetProjection( proj )
        .SetResultTransformer(Transformers.AliasToBean(typeof(Result)))
        .List<Result>();
    

    Result transformers can also be used on SQL and HQL queries.

    list2 = Session.CreateSQLQuery("select Id, Username from user_table")
        .SetResultTransformer(Transformers.AliasToBean(typeof(Result)))
        .List<Result>();
    
    list2 = Session.CreateQuery("select Id, Username from User")
        .SetResultTransformer(Transformers.AliasToBean(typeof(Result)))
        .List<Result>();
    

    In these examples the the Result class does not need to be a mapped class, and must have the selected properties.

    partial class Result
    {
        public int Id { get; set; }
        public string Username { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a entity with a id and a name fields. I want
Suppose I want to allow to select our entity (from a dropdown, etc) on
Suppose I have an entity class that stores a username: class Profile { private
I'm having the following doubt about contexts in entity framework: Suppose that there are
Suppose I have an entity in Fluent NHibernate that looks like this: public class
Suppose I have an entity like this: @Entity @Table(name = ITEMS) public class Item
This question pertains to query optimization using Linq with the Entity Framework. Is there
I want to use data from my MongoDB for displaying on HTML code via
Suppose there are three classes: One is: @Entity @Indexed @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name =
Let’s suppose there is an entity called PERSON in a CoreData model and a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.