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 988041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:34:52+00:00 2026-05-16T05:34:52+00:00

I’m reading the documentation about DetachedCriteria. The documentation clearly shows that setting an alias

  • 0

I’m reading the documentation about DetachedCriteria. The documentation clearly shows that setting an alias for your projection is optional. However, whenever I omit the alias my model properties contain no data. Here are my two test models.

[ActiveRecord("INCIDENT")]
public class Incident : ActiveRecordBase<Incident>
{
    [PrimaryKey(PrimaryKeyType.Native, "INCIDENT_ID", ColumnType = "Int32")]
    public virtual int IncidentId { get; set; }

    [Property("CREATION_DATETIME", ColumnType = "DateTime")]
    public virtual DateTime? CreationDatetime { get; set; }

    [BelongsTo("CAUSE_CD")]
    public virtual Cause Cause { get; set; }
}

[ActiveRecord("CAUSE")]
public class Cause : ActiveRecordBase<Cause>
{
    [PrimaryKey(PrimaryKeyType.Native, "CAUSE_CD", ColumnType = "String")]
    public virtual string CauseCd { get; set; }

    [Property("CAUSE_DESC", ColumnType = "String", NotNull = true)]
    public virtual string CauseDesc { get; set; }
}

Here is what I use to query the database.

DetachedCriteria incidentCriteria = DetachedCriteria.For<Incident>("i")
.SetProjection(Projections.ProjectionList()
   .Add(Projections.Property("i.IncidentId"))
   .Add(Projections.Property("i.CreationDatetime"))
)
.SetResultTransformer(Transformers.AliasToBean<Incident>());
IList<Incident> incidents = Incident.FindAll(incidentCriteria);

Both projections properties do not get populated unless I set an alias. So my question is, why is the alias optional? I’m sure I’m simply missing something else. But if I put a random alias (e.g. abc) it’ll return an error saying that it could not find property “abc” in the Incident class. Fair enough, I add the appropriate alias to match my property names. And voila! My properties are now property being populated.

Now comes the issue of when I want to query a lookup table. I add

.Add(Projections.Property("c.CauseDesc"), "CauseDesc")

to my ProjectionList and append

.CreateCriteria("i.Cause", "c")

But now it complains that it can’t find “CauseDesc” from my Incident model.

What am I missing from this whole criteria ordeal?

Update:
The following code

IList<Incident> results = sess.CreateCriteria<Incident>("i")
    .SetProjection(Projections.ProjectionList()
        .Add(Projections.Property("i.IncidentId"), "IncidentId")
        .Add(Projections.Property("i.CreationDatetime"), "CreationDatetime")
        .Add(Projections.Property("c.CauseDesc"), "CauseDesc")
    )
    .Add(Expression.Gt("i.IncidentId", 1234567))
    .CreateAlias("Cause", "c")
    .List<Incident>();

This does create a valid query (I checked it with a profiler) but it seems to be having issues populating my generic list. It gives me error “The value \”System.Object[]\” is not of type \”oms_dal.Models.Incident\” and cannot be used in this generic collection.\r\nParameter name: value”. However, all works fine if I don’t use a projection but then it selects 50+ fields which I don’t want. Does that mean I’m forced to use a DTO in this circumstance?

  • 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-16T05:34:53+00:00Added an answer on May 16, 2026 at 5:34 am

    You need to specify the projection property name like…

    .Add(Projections.Property("i.IncidentId"), "IncidentId")
    

    also, in general you do not project into the same domain object. You should create an incident dto like…

    public class IncidentDTO  
    {
        public int IncidentID { get; set; }
        public DateTime CreationDatetime { get; set; } 
    }
    

    and then…

    .SetProjection(Projections.ProjectionList()
       .Add(Projections.Property("i.IncidentId"), "IncidentId")
       .Add(Projections.Property("i.CreationDatetime"), "CreationDatetime")
    )
    .SetResultTransformer(Transformers.AliasToBean<IncidentDTO>());
    

    If you want Incidents matching some criteria (not a DTO), then don’t set projections/resulttransformer. Instead you simply do something like this…

    IList<Incident> incidents = session.CreateCriteria<Incident>()
        .CreateAlias("Cause", "c")  //now you can access Cause properties via `c.`
        .Add(Restrictions.Eq("c.CauseDesc", "some cause")) 
        .List<Incident>();
    

    See how the root criteria object doesn’t need an alias. If it helps, I only use CreateCriteria for the initial object. If I need to reference child objects, I use CreateAlias.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

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.