使用Include方法的实体框架

时间:2012-12-19 13:18:35

标签: c# linq entity-framework

我遇到包含方法问题,方案如给出

表仓库有列

  1. 标识 - >唯一标识符和PK
  2. warehouseNumber - 为nvarchar(50)
  3. - 更多列

    表WarehouselnkedEcorders

    1. Id - > PK,唯一标识符
    2. warehouseUniqueId - >(FK,唯一标识符),它与“仓库”表ID列
    3. 有关系
    4. 状态
    5. 仓库模型具有以下代码

      public class Warehouse
      {
          public Warehouse()
          {
              this.WarehouselnkedEcorders = new List<WarehouselnkedEcorder>();
          }
          //Some stuff
      
          public virtual ICollection<WarehouselnkedEcorder> WarehouselnkedEcorders 
          { get; set; }
      }
      

      WarehouselnkedEcOrdeMap具有以下代码

              // Relationships
              this.HasOptional(t => t.Warehouse)
                  .WithMany(t => t.WarehouselnkedEcorders)
                  .HasForeignKey(d => d.warehouseUniqueId);
      

      查询

      我需要检索status = true的仓库实体列表。我尝试了以下方式,但无法获得结果。我怎么能这样做?

        List<Warehouse> lstObjWarehouse = objWMSContext.Warehouses.Include("WarehouselnkedEcorders").Where(//o=>SomeCondition)
                                                             .Where(o => SomeCondition)
                                                             .Where(o => o.Deleted == false).ToList();
      

      这里我需要比较“WarehouselnkedEcorder”每行的status = true值,它应该返回结果。

2 个答案:

答案 0 :(得分:1)

在查询中使用Any:

List<Warehouse> lstObjWarehouse = db.Warehouses.Include("WarehouselnkedEcorder")
.Where(o => o.invoicePath == "SomeCondition" 
&& o.Deleted == false 
&& o.WarehouselnkedEcorders.Any(p => p.id == o.id 
&& p.Deleted == false)).ToList();

答案 1 :(得分:0)

已修复以匹配1:多个条件

List<Warehouse> lstObjWarehouse =   objWMSContext.Warehouses.Include("WarehouselnkedEcorder")
                    .Where(o => o.somefield == "SomeCondition"
                  &&       o => o.Deleted == false
                  &&       o=>o.WarehouselnkedEcorders.Where(dep=>dep.status==true)
                 .ToList();