简体   繁体   English

LINQ to SQL从字符串创建查询

[英]LINQ to SQL create query from string

I have a dbml context query that looks something like this: 我有一个dbml上下文查询,看起来像这样:

var SQLQueryResult = (from activeTable in context.activeTabless
                      where (
                               activeTable .AssignedTo == "Person1" ||
                               activeTable .AssignedTo == "Person2" ||
                               activeTable .AssignedTo == "Person3")
                    select new { ... });

My question is, how can I update the where field so that it can have any number of or (not just three as above) based on a user selection? 我的问题是,如何根据用户选择来更新where字段,使其可以具有任意数量的or (而不是上面的三个)?

Let's say the number can come from a list or array. 假设数字可以来自列表或数组。 That's simple with straight SQL but not sure how to do it via Linq to SQL. 这对于直接SQL来说很简单,但不确定如何通过Linq to SQL来实现。

var persons = new []{"Person1", "Person2", "Person3"};
var SQLQueryResult = (from activeTable in context.activeTabless
                      where ( persons.Contains(activeTable .AssignedTo))
                    select new { ... });

You can check if something exists in a collection using the .Contains() extension method of IEnumerable . 您可以使用IEnumerable.Contains()扩展方法检查集合中是否存在某些内容。

You can create your query dynamically by using Expressions for being able to build where predicates. 您可以使用表达式动态创建查询,以便能够构建谓词的位置。 More details and a sample you can find here: Linq dynamic queries 您可以在这里找到更多详细信息和示例: Linq动态查询

You can use predicate builder (utility class): 您可以使用谓词生成器(实用程序类):

using System;
using System.Linq;
using System.Linq.Expressions;

public static class PredicateBuilder {
  public static Expression<Func<T, bool>> Make<T>() { 
    return null; 
  }

  public static Expression<Func<T, bool>> Make<T>(this Expression<Func<T, bool>> predicate) {
    return predicate;
  }

  public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr, Expression<Func<T, bool>> orExpression) {
    if (expr == null) {
      return orExpression;
    }
    var invokedExpr = Expression.Invoke(orExpression, expr.Parameters.Cast<Expression>());
    return Expression.Lambda<Func<T, bool>>(Expression.Or(expr.Body, invokedExpr), expr.Parameters);
  }

  public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr, Expression<Func<T, bool>> andExpression) {
    if (expr == null) {
      return andExpression;
    }
    var invokedExpr = Expression.Invoke(andExpression, expr.Parameters.Cast<Expression>());
    return Expression.Lambda<Func<T, bool>>(Expression.And(expr.Body, invokedExpr), expr.Parameters);
  }
}

Usage: 用法:

public IEnumerable<Squad> GetSquadsByIDs(IEnumerable<int> squadIDs) {
  if (squadIDs == null || !squadIDs.Any()) {
    throw new ArgumentNullException("squadIDs");
  }
  var condition = PredicateBuilder.Make<Squad>(s => false);
  foreach (var squadID in squadIDs) {
    int squadIDValue = squadID;
    condition = PredicateBuilder.Or<Squad>(condition, s => s.SquadID == squadIDValue);
  }
  var db = m_DalContextProvider.GetContext();
  return db.Squads.Where(condition);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM