C# 如何在foreach循环中创建动态多重和linq条件 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循环中创建动态多重和linq条件 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#,linq,C#,Linq,上面的行需要很长时间才能执行,ids包含列表,查询包含linq查询 //执行查询需要花费大量时间,并且多个和条件不起作用如果我理解正确,您的问题是此查询的运行时间很长。让我们在最后一行中查看您的代码: if (rowCount == 1) { query = (from x in partJoinTableRepository.GetPartJoinQuery() join y in partRepos

上面的行需要很长时间才能执行,ids包含列表,查询包含linq查询


//执行查询需要花费大量时间,并且多个和条件不起作用

如果我理解正确,您的问题是此查询的运行时间很长。让我们在最后一行中查看您的代码:

    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();
在LINQ to SQL(和to entities)中,您的查询不会执行,尽管您使用了
ToList()
ToArray()
等。。例如,考虑下面的查询:

var count = query.Where(predicate).Distinct().ToList().Count();
最后一个查询是
从[表]中选择上限,其中的上限类似于“%A%”

在您的情况下,首先将查询发送到DB并获取与条件(
.Where()
)对应的所有对象,然后获取应用程序中的对象数

相反,如果只从数据库中获取计数,则查询速度会更快:

var strings = Db.Table
    .Where((string s) => s.Contains("A")) // Will convert to something like WHERE s LIKE '%A%'
    .Select(s => s.ToUpper()) // Will convert to something like SELECT upper(s)
    .ToList(); // Here the query sends to the DB and executes

删除
ToList
,以提高性能。因为
ToList
执行查询并将对象列表检索到内存中。但你只需要数一数。您不需要对象

var count = query.Where(predicate).Distinct().Count(); // No .ToList()! Here, .Count() executes the query.

未形成多个和条件。如果您希望我们花5分钟帮助您,请花2分钟整理您的代码。它是一个messforeach(repids中的var项){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().ToList().count();有一个编辑button@TheGeneral,我已经编辑了removed ToList()并编辑了代码,但仍然没有working@priyankha有什么问题吗?还是性能问题?另一个问题?
var count = query.Where(predicate).Distinct().Count();