0
public GetApplicants(string Office,
                         int Id,
                         List<int> cfrparts,
                         List<int> expertiseAreaIds,
                         List<int> authIds,
                         List<int> specIds)
    {
        bool isAuthIdsNull = authIds == null;
        authIds = authIds ?? new List<int>();
        bool isSpecIdNull = specIds == null;

enter code here
     var query =
            from application in Apps
            from cfr in application.cfr
            from exp in cfr.Aoe
           from auth in exp.Auth
            from spec in exp.Special

            where application.Design.Id == 14
            where  (iscfrpart || cfrPartIds.Contains(cfr.CfrP.Id))
            where (isexp || expertiseAreaIds.Contains(exp.Aoe.Id))
            where (isAuthIdsNull || authIds.Contains(auth.Auth.Id))
            where  (isSpecIdNull || specIds.Contains(spec.Special.Id))
            where application.Office.Text.Contains(Office)
            where application.D.Id == Id

            select application.Id;

我怎样才能使这个查询动态。如果我只有 Id 和 Office 值,它仍然应该根据可用值给我结果集。Cureently 它没有给我结果。

4

2 回答 2

1

不要多次调用where,而是使用&&

 var query =
        from Apps
        where (iscfrpart || cfrPartIds.Contains(Apps.cfr.CfrP.Id))
        && (isexp || expertiseAreaIds.Contains(Apps.cfr.Aoe.Id))
        && (isAuthIdsNull || authIds.Contains(Apps.cfr.Aoe.Auth.Id))
        && (isSpecIdNull || specIds.Contains(Apps.cfr.Aoe.Special.Id))
        && Apps.Office.Text.Contains(Office)
        && Apps.D.Id == Id

        select application.Id;

此外,此子句application.D.Id == 14与此子句组合时将导致 0 结果:application.D.Id == Id如果传入的 Id 不等于 14。您可能希望删除第一个子句。

编辑:更新了您的 from 子句,但我仍然认为这不会起作用,因为您的表结构似乎关闭了。

于 2013-01-27T16:23:05.117 回答
0

不需要动态查询来解决这个问题。您可以编写构造查询的代码。

制作一个基于您拥有的信息构建过滤器的方法。

public Expression<Func<Application, bool>> GetFilterForCFR(List<int> cfrParts)
{
  if (cfrParts == null || !cfrParts.Any())
  {
    return app => true;
  }
  else
  {
    return app => app.cfr.Any(cfr => cfrParts.Contains(cfr.cfrPId));
  }
}

然后,您可以使用表达式构造查询。

var query = Apps;
var cfrFilter = GetFilterForCFR(cfrParts);
query = query.Where(cfrFilter);

//TODO apply the other filters

var finalquery = query.Select(app => app.Id);
于 2013-01-27T16:32:25.603 回答