NHibernate 使用 SetProjection 检索单个集合项
我正在尝试使用投影从 Lead 类创建 DTO。例如,我的 DTO 有家庭电话号码、手机号码和电子邮件地址,但 Lead Class 有一组联系方式详细信息。因此,我试图找到一种方法来检索每个联系人详细信息,以便我可以设置 dto 上的属性。我尝试过使用子查询和投影,但无济于事。
我试图生成的 SQL 是这样的:
SELECT
A.LeadId,
B.ContactId,
B.Value,
C.ContactId,
C.Value,
D.ContactId
FROM Lead A
LEFT JOIN ContactDetail B ON A.LeadId=B.LeadId AND B.ContactType='Home Number'
LEFT JOIN ContactDetail C ON A.LeadId=C.LeadId AND C.ContactType='Mobile Number'
LEFT JOIN ContactDetail D ON A.LeadId=D.LeadId AND D.ContactType='Email Address'
所以简而言之,我尝试根据不同的条件加入同一个表 3 次,而且我知道在 NHibernate 中我不能使用 CreateAlias 加入同一个表表不止一次,我很想知道是否可以使用 Criteria API 或 NHIbernate Linq。预先感谢您的任何帮助。
I am trying to use Projections to Create a DTO from a Lead class. My DTO has, for example, a Home phone number, a mobile number and an email address, but the Lead Class has a collection of contact details. So I'm trying to find a way to retrieve each contact detail so that I can set the properties on the dto. I have tried using sub queries and projections but to no avail.
The SQL I'm trying to generate is something like this:
SELECT
A.LeadId,
B.ContactId,
B.Value,
C.ContactId,
C.Value,
D.ContactId
FROM Lead A
LEFT JOIN ContactDetail B ON A.LeadId=B.LeadId AND B.ContactType='Home Number'
LEFT JOIN ContactDetail C ON A.LeadId=C.LeadId AND C.ContactType='Mobile Number'
LEFT JOIN ContactDetail D ON A.LeadId=D.LeadId AND D.ContactType='Email Address'
So in short I'm trying to join to the same table 3 times based on different criteria, and I know that in NHibernate I can't use CreateAlias to join to the same table more than once and I'm anxious to know if this is possible using either the Criteria API or NHIbernate Linq. Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将从 nHibernate 加载“常规”主导实例及其联系方式详细信息集合。然后我会使用 AutoMapper 将其映射到 DTO 类。
在我看来,这是一种更简洁的方法,因为您不必仅为“简单”DTO 映射创建特殊的数据访问方法。而且重构更容易,因为所有内容都是通过“c Sharp 代码”表达的。
链接到 AutoMapper
I would load the "regular" lead instance from nHibernate with its contact detail collection. Then I would use AutoMapper, to map it to the DTO class.
In my opinion, this is a much cleaner approach since you do not create special data access methods just for "simple" DTO mappings. And it's easier for refactoring since everything is expressed via "c sharp code".
Link to AutoMapper