C# 谓词在foreach循环中不起作用 if(行计数==1) { 质疑= (来自partJoinTableRepository.GetPartJoinQuery()中的x) 在partRepository.GetPartsQuery()中加入y,x.PartId等于y.Id 在x.PartId等于z.PartId上的partProductTypeReposiotry.GetPartProductTypesQuery()中加入z 其中y.isskiped==0&&(y.IsDisabled!=“y”| | y.IsDisabled==null)&&z.CreatedDate==x.CreatedDate &&x.CreatedDate==Convert.ToDateTime(fromDate)&&cpaclassids.Contains(x.ProductTypeId.ToString()) 选择x).Cast().AsQueryable(); 谓词=PredicateBuilder.True(查询); } 其他的 { query=query.Join(partJoinTableRepository.GetPartJoinQuery(),“PartID”,“PartID”,“inner”,“row1”,null)。Cast().AsQueryable(); //谓词=PredicateBuilder.True(查询); }//查询包含多个动态内部联接 //repids包含列表,我使用linq的谓词生成器来创建和查询 foreach(repid中的var项) { 谓词=PredicateBuilder.True(查询); 如果(类型ID=“3”) { 谓词=谓词。和(z=>ids.Contains(z.ProductTypeId.ToString())&& z、 CreatedDate==Convert.ToDateTime(fromDate)); } } var count=query.Where(谓词).Distinct().count();

C# 谓词在foreach循环中不起作用 if(行计数==1) { 质疑= (来自partJoinTableRepository.GetPartJoinQuery()中的x) 在partRepository.GetPartsQuery()中加入y,x.PartId等于y.Id 在x.PartId等于z.PartId上的partProductTypeReposiotry.GetPartProductTypesQuery()中加入z 其中y.isskiped==0&&(y.IsDisabled!=“y”| | y.IsDisabled==null)&&z.CreatedDate==x.CreatedDate &&x.CreatedDate==Convert.ToDateTime(fromDate)&&cpaclassids.Contains(x.ProductTypeId.ToString()) 选择x).Cast().AsQueryable(); 谓词=PredicateBuilder.True(查询); } 其他的 { query=query.Join(partJoinTableRepository.GetPartJoinQuery(),“PartID”,“PartID”,“inner”,“row1”,null)。Cast().AsQueryable(); //谓词=PredicateBuilder.True(查询); }//查询包含多个动态内部联接 //repids包含列表,我使用linq的谓词生成器来创建和查询 foreach(repid中的var项) { 谓词=PredicateBuilder.True(查询); 如果(类型ID=“3”) { 谓词=谓词。和(z=>ids.Contains(z.ProductTypeId.ToString())&& z、 CreatedDate==Convert.ToDateTime(fromDate)); } } var count=query.Where(谓词).Distinct().count();,c#,C#,这里谓词不附加多个And条件。它接受最后一个And条件,而其他And条件被消除。您正在覆盖每个循环的谓词 这应该行得通 if (rowCount == 1) { query = (from x in partJoinTableRepository.GetPartJoinQuery() join y in partRepository.GetPartsQuery() on x.PartId equals y.I

这里谓词不附加多个And条件。它接受最后一个And条件,而其他And条件被消除。

您正在覆盖每个循环的谓词

这应该行得通

 if (rowCount == 1)
    {
       query =
               (from x in partJoinTableRepository.GetPartJoinQuery()
                 join y in partRepository.GetPartsQuery() on x.PartId equals y.Id
                               join z in partProductTypeReposiotry.GetPartProductTypesQuery() on x.PartId equals z.PartId
                               where y.IsSkipped == 0 && (y.IsDisabled != "Y" || y.IsDisabled == null) && z.CreatedDate == x.CreatedDate
                               && x.CreatedDate == Convert.ToDateTime(fromDate) && cpaclassids.Contains(x.ProductTypeId.ToString())
                               select x).Cast<PartJoinTable>().AsQueryable();
                      predicate = PredicateBuilder.True(query);
   }
   else
   {   
    query = query.Join(partJoinTableRepository.GetPartJoinQuery(), "PartID", "PartID", "inner", "row1", null).Cast<PartJoinTable>().AsQueryable();
                        // predicate = PredicateBuilder.True(query);
   } //query contains multiple dynamic inner joins
       //repids contains the list ,I used the predicate builder for the linq to create AND Queries
   foreach(var item in repids)
   { 
      predicate = PredicateBuilder.True(query);
      if (typeid == "3")
      {
       predicate = predicate.And(z => ids.Contains(z.ProductTypeId.ToString()) && 
               z.CreatedDate == Convert.ToDateTime(fromDate));                            
      }
  }
var count = query.Where(predicate).Distinct().Count();

您正在覆盖每个循环的谓词

这应该行得通

 if (rowCount == 1)
    {
       query =
               (from x in partJoinTableRepository.GetPartJoinQuery()
                 join y in partRepository.GetPartsQuery() on x.PartId equals y.Id
                               join z in partProductTypeReposiotry.GetPartProductTypesQuery() on x.PartId equals z.PartId
                               where y.IsSkipped == 0 && (y.IsDisabled != "Y" || y.IsDisabled == null) && z.CreatedDate == x.CreatedDate
                               && x.CreatedDate == Convert.ToDateTime(fromDate) && cpaclassids.Contains(x.ProductTypeId.ToString())
                               select x).Cast<PartJoinTable>().AsQueryable();
                      predicate = PredicateBuilder.True(query);
   }
   else
   {   
    query = query.Join(partJoinTableRepository.GetPartJoinQuery(), "PartID", "PartID", "inner", "row1", null).Cast<PartJoinTable>().AsQueryable();
                        // predicate = PredicateBuilder.True(query);
   } //query contains multiple dynamic inner joins
       //repids contains the list ,I used the predicate builder for the linq to create AND Queries
   foreach(var item in repids)
   { 
      predicate = PredicateBuilder.True(query);
      if (typeid == "3")
      {
       predicate = predicate.And(z => ids.Contains(z.ProductTypeId.ToString()) && 
               z.CreatedDate == Convert.ToDateTime(fromDate));                            
      }
  }
var count = query.Where(predicate).Distinct().Count();