1

I tired this

respondentSanctionSubquery = respondentSanctionSubquery.Select(x => x.Respondent.Incident.Id);

but i got this exception : enter image description here

i have 3 entities not 2 entities :

class Respondent
{
public IncidentObj{get;set;}
}
class Incident
{
public int Id{get;set;}
}
class RespondentSanction
{
public Respondent RespondentObj{get;set;}
}
sohaib
  • 113
  • 1
  • 7

3 Answers3

1

You have to do a JOIN in order to do a projection like that:

respondentSanctionSubquery = 
    respondentSanctionSubquery
        .JoinQueryOver(x => x.RespondentObj)
        .JoinQueryOver(resp => resp.IncidentObj)
        .Select(inc => inc.Id);
Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
  • This work only with two entity , but I have 3 entities related with each other please find the updated question . – sohaib Feb 10 '15 at 07:45
  • also please not the I'm trying to do this in Sub Query .(this one respondentSanctionSubquery is sub query ). – sohaib Feb 10 '15 at 07:48
  • Actually i tired this but it dose not work , but i find the solution i used .JoinAlias instead of JoinQueryOver. – sohaib Feb 10 '15 at 14:01
  • anyway Andrew thank you for your help . I appreciate your help – sohaib Feb 10 '15 at 14:02
  • now the query can't resolve the RespondentSanction properties – sohaib Feb 10 '15 at 14:06
  • @sohaib: Oh, I see. You must be selecting other properties that aren't in your question. In that case, `JoinAlias` is a better option. – Andrew Whitaker Feb 10 '15 at 14:07
  • no the other properties I used them in where statement not in selection :) – sohaib Feb 10 '15 at 14:09
  • @sohaib: I see-- in that case you can put the `Where` clauses right after the `respondentSanctionSubquery`, but `JoinAlias` is easier to use here and if it works, I'd stick with it. Glad you figured it out – Andrew Whitaker Feb 10 '15 at 14:09
1

You have to join other entities also to the main query (as follows),

X x = null; 
Respondent respondent = null;
Incident incident = null;

respondentSanctionSubquery = respondentSanctionSubquery
        .JoinQueryOver(() => x.Respondent , () => respondent)
        .JoinQueryOver(() => respondent.Incident , () => incident )
        .Select(r => incident.Id);

or else you might want to go for subqueries,

X x = null; 
Respondent respondent = null;
Incident incident = null;

    var subQuery = (QueryOver<Respondent>)session.QueryOver<Respondent>(() => respondent)
                  .JoinQueryOver(() => respondent.Incident , () => incident )
                  .Where(() => respondent.Id == x.Respondent.Id)
                  .Select(r => incident.Id);

    var query = session.QueryOver(() => x)
                .SelectList(l => l.SelectSubQuery(subQuery));
Low Flying Pelican
  • 5,974
  • 1
  • 32
  • 43
  • i tried this but i got this exception 'Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. ' – sohaib Feb 10 '15 at 10:03
  • Obviously you have more than one incident per x and that's the reason you are getting this error, so you probably should go for the first option. – Low Flying Pelican Feb 10 '15 at 10:06
  • could you please see the question again i did some changes on it . – sohaib Feb 10 '15 at 12:43
  • Also i tired the first solution but another problem is appeared . I got this exception 'could not resolve property: StartDate of: Entities.Incident' note that the Start Date is exist in the RespondentSanction entity .any advice please ? – sohaib Feb 10 '15 at 12:45
  • Are you sure your mapping is in order? Sounds like issue with mapping of the start date. – Low Flying Pelican Feb 10 '15 at 22:51
1

you should do join between the entities using Join alias

respondentSanctionSubquery = 
    respondentSanctionSubquery
        .JoinAlias(x => x.RespondentObj)
        .JoinAlias(resp => resp.IncidentObj)
        .Select(inc => inc.Id);

for more information please check this URL :What is the difference between JoinQueryOver and JoinAlias?

Community
  • 1
  • 1
sohaib
  • 113
  • 1
  • 7