1

I have two classes

Public class Foo 
{ 
   int Id {get;set;}
   Email DefaultEmail {get;set;} 
}

Public class Email 
{ 
   string main {get;set;} 
   string backup {get;set;}
   string recover {get;set;}
}

 Public string GetEmail(EnType)
 {
      switch(EnType)
      {
            Case EnType.Type1:
                return this.main;

            // others condition....
      }
 }

I map Foo to Email using component map. Each time only one email is required.

when I select

Session.Query.select(x => x.email.getEmail(EnType.Type1))

SQL generated is

select emailType1, emailType2,.... From Foo

while I expect only

select emailType1 From Foo
Jec
  • 59
  • 3
  • 2
    While this may not be a popular answer there are a lot of faults with linq to Nhibernate (try joins) it does lots of things you wouldnt expect. I would recommend using either the Criteria or QueryOver in order to get what you want if you want to control how NHibernate writes queries. – Mark Broadhurst Aug 14 '12 at 07:03

1 Answers1

0

GetEmail() is implemented in code which is located in the Email class. How do you think nhibernate should know what the code is supposed to be doing?

It just fetches the whole email class (component) and then let it decide what to return.

Firo
  • 30,626
  • 4
  • 55
  • 94
  • What would be the best way to if I would like to limit the column selected? – Jec Aug 14 '12 at 09:31
  • you would need to reimplement the logic in the query and project only the property you actually want – Firo Aug 14 '12 at 19:34