2011-11-16 35 views
15

Tôi có một câu hỏi như thế nàyLINQ to bản sao Nhibernate tham gia

var orderedQueryable = this.participationRequests 
      .Fetch(x => x.CommunityEvent) 
      .Fetch(x => x.CommunityMember) 
       .ThenFetch(x => x.User) 
      .Where(x => x.CommunityMember.Community.Id == communityId) 
      .OrderBy(x => x.CreateDate); 

Các mệnh đề where cần phải sau lấy do this bug. Vấn đề là thây Fetch cuộc gọi đưa ra các kết nối bổ sung. Trong truy vấn SQL trông giống như sau:

select * 
from ParticipationRequests participat0_ 
     left outer join CommunityEvents communitye1_ 
     on participat0_.CommunityEventId = communitye1_.Id 
     left outer join CommunityMembers communitym2_ 
     on participat0_.CommunityMemberId = communitym2_.Id 
     left outer join Users user3_ 
     on communitym2_.UserId = user3_.Id 
     inner join CommunityMembers communitym4_ 
     on participat0_.CommunityMemberId = communitym4_.Id 
     inner join CommunityMembers communitym5_ 
     on participat0_.CommunityMemberId = communitym5_.Id 
     inner join Communities community6_ 
     on communitym5_.CommunityId = community6_.Id 
where community6_.Id = 2002 /* @p0 */ 
order by participat0_.CreateDate asc 

Nó tham gia bên trong để đặt điều kiện trên CommunityId và không tham gia bên ngoài để tìm nạp.

Tôi đã tìm thấy similar question, nhưng truy vấn của tôi có kế hoạch thực hiện khác nhau có và không có sự tham gia bổ sung.

Đây có phải là lỗi trong nhà cung cấp LINQ không? Có thể có một cách giải quyết?

Trả lời

1

Không chắc chắn chính xác nhưng loại bỏ nơi truy vấn của bạn và thay vào đó sử dụng một tham gia một cái gì đó trên dòng

tham gia o trong x.CommunityMember.Community trên communityId bằng x.communityMember.Community.Id (cú pháp của tôi là comp0letely ra , nhưng có thể phục vụ bạn như một gợi ý)

1

như Sly đề cập, đây là một vấn đề được biết đến với LINQ to NHibernate.

Tôi đã có thể giải quyết vấn đề này bằng cách sử dụng HQL thay vì LINQ. Kết quả của bạn sẽ giống như sau:

CommunityEvent ce = null; 
CommunityMember cm = null; 
var queryable = this.participationRequests 
    .JoinAlias(x => x.CommunityEvent,() => ce) 
    .JoinAlias(x => x.CommunityMember,() => cm) 
    .Where(() => cm.Community.Id == communityId) 
    .OrderBy(x => x.CreationDate);