如何使用linq和多对多表进行左外连接?

时间:2014-03-23 00:58:54

标签: c# model-view-controller linq-to-sql ef-migrations fluent

当我尝试使用MVC代码进行左外连接时,我得到了这个错误:

  

join子句中某个表达式的类型不正确。   调用'GroupJoin'时类型推断失败。

我需要帮助才能理解join子句错误的原因。这是代码:

return (from t in context.Tenders
                    join c in context.Contacts on t.Contacts equals c.Tenders into leftOuter
                    from subpet in leftOuter.DefaultIfEmpty()
                    orderby t.id
                    where (c.Email == currentUserProfile.Email || c.UserProfileId == currentUserProfile.Id || t.UserProfileId == currentUserProfile.Id) && t.EndDate > now
                                         && t.IsClosed == false
                                         && t.IsCancel == false
                    select t).Union(
                    from t in context.Tenders
                    where t.EndDate > now
                    && t.IsClosed == false
                    && t.IsCancel == false
                    && t.PrivacyLevelId == 1
                    select t).Count();

Fluent API:

// Many to many Tender => Contact
        modelBuilder.Entity<Tender>()
            .HasMany(c => c.Contacts)
            .WithMany(t => t.Tenders)
            .Map(m => m.MapLeftKey("TenderId")
                 .MapRightKey("ContactId")
                 .ToTable("ContactTender"));

0 个答案:

没有答案