使用嵌套的Any运算符查询五个表

时间:2014-01-30 17:15:48

标签: c# linq entity-framework entity-framework-5

我正在尝试创建单个查询以获取属于某个公告的所有用户,但我似乎无法让我的查询起作用。

我收到以下错误:

  

无法创建“RequestSupplierEntity”类型的常量值。   此处仅支持原始类型或枚举类型   上下文。

我不确切地知道错误消息的含义,或者我应该做什么/避免什么来阻止它。任何对问题的帮助或洞察,甚至是工作查询,都将不胜感激。 在我看来,使用EXIST子查询应该可以进行单个T-SQL查询,我只是不知道实体框架是否能够在这种情况下构造它。

我的查询声明:

Users.Where(u => notification.Announcement.RequestSuppliers.Any(rs => rs.Supplier.OrganisationId == u.OrganisationId));

用户是DBSet。 通知是我正在尝试查找关联用户的实体实例。我觉得问题在于使用DBSet的 where 方法中的NotificationEntity实例,但我不知道我应该怎么做。

POCO实体涉及如下:

  • 用户有一个组织
  • 供应商是一个组织
  • RequestSupplier是供应商<​​/ li>
  • RequestSupplier有一个公告
  • 通知有公告

所以我要做的就是通过RequestSupplier获取属于与公告相关联的供应商的OrganisationId用户。

关联的FluentAPI是:

modelBuilder.Entity<NotificationEntity>()
    .HasOptional<AnnouncementEntity>(n => n.Announcement)
    .WithMany(a => a.Notifications)
    .HasForeignKey(n => n.AnnouncementId)
    .WillCascadeOnDelete(false);
modelBuilder.Entity<RequestSupplierEntity>()
    .HasRequired<SupplierEntity>(rs => rs.Supplier)
    .WithMany(s => s.RequestSuppliers)
    .WillCascadeOnDelete(false);
modelBuilder.Entity<RequestSupplierEntity>()
    .HasKey(rs => new { rs.SupplierId });
modelBuilder.Entity<RequestSupplierEntity>()
    .HasRequired<AnnouncementEntity>(rs => rs.Announcement)
    .WithMany(a => a.RequestSuppliers)
    .HasForeignKey(rs => rs.AnnouncementId)
    .WillCascadeOnDelete(false);
modelBuilder.Entity<SupplierEntity>()
    .HasRequired<OrganisationEntity>(s => s.Organisation)
    .WithMany(o => o.Suppliers)
    .HasForeignKey(s => s.OrganisationId)
    .WillCascadeOnDelete(false);
modelBuilder.Entity<UserEntity>()
    .HasOptional<OrganisationEntity>(u => u.Organisation)
    .WithMany(o => o.Users)
    .WillCascadeOnDelete(true);

我正在执行此查询的通知始终具有关联的公告。

实体:

[Table("Announcement")]
public class AnnouncementEntity
{
    public int Id { get; set; }

    public virtual ICollection<RequestSupplierEntity> RequestSuppliers { get; set; }
    public virtual ICollection<NotificationEntity> Notifications { get; set; }
}

[Table("Notification")]
public class NotificationEntity
{
    public int Id { get; set; }
    public int? AnnouncementId { get; set; }

    public virtual AnnouncementEntity Announcement { get; set; }
}

[Table("Organisation")]
public class OrganisationEntity
{
    public int Id { get; set; }

    public virtual ICollection<SupplierEntity> Suppliers { get; set; }
    public virtual ICollection<UserEntity> Users { get; set; }
}

[Table("RequestSupplier")]
public class RequestSupplierEntity
{
    public int SupplierId { get; set; }
    public int AnnouncementId { get; set; }

    public virtual SupplierEntity Supplier { get; set; }
    public virtual AnnouncementEntity Announcement { get; set; }
}

[Table("Supplier")]
public class SupplierEntity
{
    public int Id { get; set; }
    public int OrganisationId { get; set; }

    public virtual OrganisationEntity Organisation { get; set; }
    public virtual ICollection<RequestSupplierEntity> RequestSuppliers { get; set; }
}

[Table("User")]
public class UserEntity
{
    public int Id { get; set; }
    public int? OrganisationId { get; set; }

    public virtual OrganisationEntity Organisation { get; set; }
}

1 个答案:

答案 0 :(得分:1)

我不确定但是试试这个

  db.Users.Where(u => db.Notifications.Select(n=>n.Announcement).SelectMany(a=>a.RequestSuppliers).Any(rs => rs.Supplier.OrganisationId == u.OrganisationId));

我猜你试图制作一个RequestSuppliers集合的集合,这些集合是通过Annoncement从Where()方法中的Notification集合中选择的。   在这种情况下,您必须使用SelectMany

在任何情况下都可以使用join

var users=(from u in db.Users
         join s in db.Suppliers
         on u.OrganisationId equals s.OrganisationId
         join rs in db.RequestSuppliers
         on s.Id equals rs.SupplierId
         join a in db.Announcements
         on rs.AnnouncementId equals a.Id
         join n in db.Notifications
         on a.Id equals n.AnnouncementId
         select u);