linq - 如何使用流利的 linq 获取当前不在映射表中的对象

标签 linq entity-framework ef-code-first fluent

我正在尝试使用 fluent linq 获取当前未映射到购物车的所有 Catalog 对象。映射表 cart_catalog_mapping 由 EF 生成。我使用以下域对象。

购物车(删除了与问题无关的评论和额外字段

public partial class Cart : BaseEntity, ILocalizedEntity
    {

        public virtual string Name { get; set; }

        public virtual DateTime OpeningDateUtc { get; set; }

        public virtual DateTime ClosingDateUtc { get; set; }

        public virtual bool IsReadonly { get; set; }

        public virtual bool IsPreviewMode { get; set; }

        public virtual CustomerRole CustomerType { get; set; }

        public virtual ICollection<Catalog> Catalogs { get; set; }

    }

目录(再次删除与问题无关的评论和额外字段)

public partial class Catalog : BaseEntity, ILocalizedEntity
    {
        public virtual string Name { get; set; }

        public virtual string Code { get; set; }

        public virtual bool Published { get; set; }

        public virtual int DisplayOrder { get; set; }
    }

带代码的 EF5 首先创建购物车和目录表。它还识别出购物车具有列表并创建一个 cart_catalog_mapping 表。 我正在尝试获取 Catalog 表中没有在 cart_catalog_mapping 表中引用的所有行。我设想的 SQL 是

SELECT * FROM Catalog WHERE Catalog.Id NOT IN (SELECT Catalog_Id FROM cart_catalog_mapping)

我尝试使用的流利的 linq 如下: 公共(public) IList GetAllUnassociatedCatalogs() { IRepository _catalogRepository; IRepository _cartRepository;

    var query = from catalog in _catalogRepository.Table
                    from cart in _cartRepository.Table
                    where !cart.Catalogs.Contains(catalog)
                    select catalog;

    return query.ToList();

很遗憾,返回的 IList 没有元素。 (目前在 cart 或 cart_catalog_mapping 表中没有行,在 catalog 表中有 3 行)

差点忘了,映射表

public partial class CartMap : EntityTypeConfiguration<Cart>
    {
        public CartMap()
        {
            this.ToTable("Cart");
            this.HasKey(c => c.Id);
            this.Property(c => c.Name).IsRequired().HasMaxLength(100);
            this.Property(c => c.IsOnline).IsRequired();

            this.HasMany(c => c.Catalogs)
                .WithMany()
                .Map(m => m.ToTable("Cart_Catalog_Mapping"));
        }
    }

public partial class CatalogMap : EntityTypeConfiguration<Catalog>
    {
        public CatalogMap()
        {
            this.ToTable("Catalog");
            this.HasKey(c => c.Id);
            this.Property(c => c.Name).IsRequired().HasMaxLength(100);
        }

    }

最佳答案

根据您的示例,我假设 Cart 和 Catalog 是多对多的。在这种情况下,您是否不能也将 ICollection of Carts 放在 Catalog 对象上?如果你能做到,那么你需要做的就是:

var query = catalogRepository.Any(catalog => catalog.Carts.Any());
return query.ToList();

或者,如果您更喜欢不使用扩展方法,您可以按照@hvd 的建议进行操作。

关于linq - 如何使用流利的 linq 获取当前不在映射表中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13959922/

相关文章:

c# - 索引超出了 lambda 表达式 linq 中的数组边界

database - 在数据访问 block 5.0 或 Entity Framework 4.0 之间进行选择

entity-framework - Entity Framework Core 是否有一种简单的方法来阻止子实体或父实体的更新?

mysql - 如何将 Entity Framework 与 MySQL Connector noinstall 版本一起使用?

c# - 可空 DateTime 与依赖默认值 ( DateTIme )

c# - IEnumerable<IEnumerable<T>> 到 string[][]

c# - 使用 lambda 表达式在 linq 中加入 outer

linq - 编写自定义 NHibernate HQL 生成器时如何访问参数属性

entity-framework-4 - 这个映射有什么问题吗?

entity-framework - 如何重命名应用于数据库的最后一个 Entity Framework 迁移