Linq语句Where在Where中

Linq语句Where在Where中,linq,Linq,我正在构建一个查询工具,供非技术人员从数据库中检索记录 我有一个带有各种下拉列表的表单,用户可以根据他们要找的内容来选择 我遇到了一个问题,查询返回的记录与用户选择不匹配 我相信只有在查询联接表时才会发生这种情况 我有以下资料: results = results.Where(c => c.CustomerEnrollment .Where(x => x.CustomerCategoryID == CustomerCategoryID)

我正在构建一个查询工具,供非技术人员从数据库中检索记录

我有一个带有各种下拉列表的表单,用户可以根据他们要找的内容来选择

我遇到了一个问题,查询返回的记录与用户选择不匹配

我相信只有在查询联接表时才会发生这种情况

我有以下资料:

results = results.Where(c => c.CustomerEnrollment
                  .Where(x => x.CustomerCategoryID == CustomerCategoryID)
                  .Any());

results = results.Where(c => c.CustomerEnrollment
              .Where(x => x.StartDate <= DateRangeStart && x.EndDate >= DateRangeStart)
              .Any());

尝试将日期范围检查更改为

更改:

x => x.StartDate <= DateRangeStart && x.EndDate >= DateRangeStart
x=>x.StartDate=DateRangeStart
致:

//开始日期应大于或等于
//EndDate应小于或等于
//此外,您还使用相同的变量DateRangeStart来检查开始和结束

x=>x.StartDate>=DateRangeStart&&x.EndDate您的查询返回的类别中,有任何
CustomerRolliment
具有其Id,并且在所需的数据范围中也有任何
CustomerRolliment
,但这些
CustomerRolliment
不一定相同

要确保使用
CustomerRolliment
s获得满足这两个条件的类别,您必须执行以下操作:

results = results.Where(c => c.CustomerEnrollment
                  .Where(x => x.CustomerCategoryID == CustomerCategoryID
                           && x.StartDate <= DateRangeStart
                           && x.EndDate >= DateRangeStart)
                  .Any());
results=results.Where(c=>c.CustomerRolliment)
其中(x=>x.CustomerCategoryID==CustomerCategoryID
&&x.StartDate=DateRangeStart)
.Any());
使用,可以参数化条件:

using LinqKit;
...

var pred = Predicate.True<CustomerEnrollment>();

if (CustomerCategoryID > 0)
    pred = pred.And(c => c.CustomerCategoryID == CustomerCategoryID);

if (DateRangeStart.HasValue)
    pred = pred.And(c => c.StartDate <= DateRangeStart
                      && c.EndDate >= DateRangeStart);

results = results.AsExpandable()
                 .Where(c => c.CustomerEnrollment.AsQueryable()
                     .Any(pred));
使用LinqKit;
...
var pred=Predicate.True();
如果(CustomerCategoryID>0)
pred=pred.And(c=>c.CustomerCategoryID==CustomerCategoryID);
if(DateRangeStart.HasValue)
pred=pred.And(c=>c.StartDate=DateRangeStart);
results=results.AsExpandable()
.Where(c=>c.customerenRolliment.AsQueryable()
。任何(pred));

.AsExpandable()
.AsQueryable()
的组合似乎是避免异常的唯一方法。

你能用文字描述一下你想做什么吗?返回已在特定日期范围外注册的特定类别的客户?您能否提供一些我们可以查找的数据?我想返回已在特定类别中注册且注册在特定日期内的客户。我的问题是,我正在根据用户选择动态生成查询。有解决办法吗?是这方面的工具。我现在没有时间,但如果你愿意,我可以稍后再试一试。同时你也可以看看。我已经试过了,但不确定如何在连接的表中使用它。如果你有时间,我会感谢你的帮助。谢谢你。我现在遇到以下错误:无法将此行的类型“System.Collections.Generic.ICollection”隐式转换为“bool”,results=results.Where(c=>c.customerenRolliment).Any(pred.Expand());抱歉,不确定您所指的是哪个查询。代码在最后一行之前一直运行良好。您的代码中缺少一个结束括号,在我得到的最后插入了这个括号。错误不包含任何错误的定义。
results = results.Where(c => c.CustomerEnrollment
                  .Where(x => x.CustomerCategoryID == CustomerCategoryID
                           && x.StartDate <= DateRangeStart
                           && x.EndDate >= DateRangeStart)
                  .Any());
using LinqKit;
...

var pred = Predicate.True<CustomerEnrollment>();

if (CustomerCategoryID > 0)
    pred = pred.And(c => c.CustomerCategoryID == CustomerCategoryID);

if (DateRangeStart.HasValue)
    pred = pred.And(c => c.StartDate <= DateRangeStart
                      && c.EndDate >= DateRangeStart);

results = results.AsExpandable()
                 .Where(c => c.CustomerEnrollment.AsQueryable()
                     .Any(pred));