1

When I try to select properties from a subquery (as in QueryOver: select columns from subquery) of type DateTime I get the following exception:

The type System.DateTime can not be assigned to a property of type System.Int32

The code looks like this:

var subQuery = QueryOver.Of<Model>().Where(x => x.ForeignKey == someId);

mainQuery.SelectList(s1 =>
                    s1.SelectSubQuery(subQuery.Select(x => x.ChangeDate)).WithAlias(() => mainQueryAlias.ChangeDate)
                      .Select(...) //properties from mainQuery

mainQuery.TransformUsing(Transformers.AliasToBean<MainModel>());

where ChangeDate is of type DateTime.

The exception then occurs in:

var expectResultList = mainQuery.List();

When I remove the ChangeDate from the select-list it works. Other properties (like type int / string from the subquery can be successfully selected as well. I think I must be missing some conversion somewhere but I have no clue where.

SOLUTION:

The problem was that I was using the same subQuery more than one times for selecting different values of the subquery:

s1.SelectSubQuery(subQuery.Select(smth.).WithAlias(some alias)
  .SelectSubQuery(subQuery.Select(smth. else).WithAlias(other alias)

However after executing one select, somehow the next select statements would use the wrong type (from prevous query?!?) for the current query...

The solution is to just put .Clone before every select to keep the query in its default state:

s1.SelectSubQuery(subQuery.Clone().Select(smth.).WithAlias(some alias)
  .SelectSubQuery(subQuery.Clone().Select(smth. else).WithAlias(other alias)
Community
  • 1
  • 1
Philipp
  • 649
  • 2
  • 7
  • 23
  • 1
    The concept is correct. For sure. There is a hidden "typo" in your code, I'd say. Check [this](http://stackoverflow.com/a/24514888/1679310) to asure that you are on the right way. SO: I would twice assure that the DTO and the return projectioon (WithAlias) really do match. If you observe the snippet you passed, there is `mainQuery` and `m_Query`... so these two do not relate... – Radim Köhler Oct 09 '14 at 14:11

1 Answers1

1

The problem was that I was using the same subQuery more than one times for selecting different values of the subquery:

s1.SelectSubQuery(subQuery.Select(smth.)     .WithAlias(some alias)
  .SelectSubQuery(subQuery.Select(smth. else).WithAlias(other alias)

However after executing one select, somehow the next select statements would use the wrong type (from prevoius query?!?) for the current query...

The solution is to just put .Clone before every select to keep the query in its default state:

s1.SelectSubQuery(subQuery.Clone().Select(smth.)     .WithAlias(some alias)
  .SelectSubQuery(subQuery.Clone().Select(smth. else).WithAlias(other alias)

Lessons learned:

Always use .Clone() if you want to execute the same query multiple times or create separate queries!

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Philipp
  • 649
  • 2
  • 7
  • 23