简体   繁体   English

LINQ查询以检查表中所有列中的谓词

[英]LINQ Query to check for a predicate in all columns in a table

I have a table with 30 columns, and it contains 1000 rows. 我有一个包含30列的表,它包含1000行。 I want a single LINQ query, which checks for a particular value in all columns and converts the result into a list. 我想要一个LINQ查询,它检查所有列中的特定值并将结果转换为列表。

For example: 例如:

table.where(allcolumnvalue.contains(searchvalue)).Tolist()

How to accomplish the above using one LINQ query. 如何使用一个LINQ查询完成上述操作。 Any help is much appreciated. 任何帮助深表感谢。

For your request all of fields should have same type, at least in the static typed C#. 对于您的请求,所有字段应该具有相同的类型,至少在静态类型的C#中。

The method Queriable.Where gets the Expression<Func<T, bool>> predicate as parameter. 方法Queriable.Where得到Expression<Func<T, bool>>谓词作为参数。 So you need build the predicate o.p1 == val || o.p2 == val || o.p3 = val ... 所以你需要构建谓词o.p1 == val || o.p2 == val || o.p3 = val ... o.p1 == val || o.p2 == val || o.p3 = val ... o.p1 == val || o.p2 == val || o.p3 = val ... as Expression value. o.p1 == val || o.p2 == val || o.p3 = val ...作为Expression值。 Here o is a parameter of Expression<Func<T, bool>> : 这里oExpression<Func<T, bool>>

public Expression BuildExpression<TObj, TVal>(TObj obj, TVal val)
{
    Expression<Func<TObj, bool>> predicate = (o) => o.p1 == val || ... || o.pN == val;
    return predicate;
}

but we need build predicate dynamically for all properties of TObj that have type TVal . 但是我们需要动态地为具有TObj类型的TVal所有属性构建谓词。

To simplify the code we will build equal expression false || o.p1 == val || ... || o.pN == val 为了简化代码,我们将构建相等的表达式false || o.p1 == val || ... || o.pN == val false || o.p1 == val || ... || o.pN == val false || o.p1 == val || ... || o.pN == val . false || o.p1 == val || ... || o.pN == val

public Expression<Func<TObj, bool>> BuildExpression<TObj, TVal>(TVal val)
{
    var parameter = Expression.Parameter(typeof(TObj), "o");
    var valExpression = Expression.Constant(val, typeof(TVal));
    var body = Expression.Constant(false, typeof(bool));

    var properties = typeof(TObj).GetProperties()
                                 .Where(p => p.PropertyType == typeof(TVal));
    foreach (var property in properties)
    {
        var propertyExpression = Expression.Property(parameter, property);
        var equalExpression = Expression.Equal(propertyExpression, valExpression);
        body = Expression.Or(body, equalExpression);
    }

    return Expression.Lambda<Func<TObj, bool>>(body, parameter);
}

. . .

using (var dbContext = new DbContext())
{
    var whereExpression = BuildExpression<User, string>("foo");
    var contaningsFoo = dbContext.Users.Where(whereExpression);
}

I got answer But Is not perfect answer But Is Worked well 我得到了答案但是不是完美的答案但是工作得很好

   public class GenericList<T>
{
    void Add(T input) { }

    public List<T> SerachFun(List<T> input, string search)
    {
        List<T> output = new System.Collections.Generic.List<T>();
        foreach (var aa in input)
        {
            var columns = aa.GetType().GetProperties().ToList();
            foreach (var bb in columns)
            {
                var cccc = bb.GetValue(aa);
                bool result = cccc.ToString().Contains(search);
                if (result)
                {
                    output.Add(aa);
                    continue;
                }

            }

        }
        return output;
    }
}

The Generic Class Object Created 创建的通用类对象

        public GenericList<table1> g = new GenericList<table1>();

the Generic Class Method Called : 被称为通用类方法:

   var tabledetails=db.table1.ToList();
   var resultcommonsearch = g.SerachFun(tabledetails, "Dhoni");

using code 使用代码

public class GenericList<T>
{
     public List<T> SerachFun(List<T> input, string search)
     {
            List<T> output = new System.Collections.Generic.List<T>();
            foreach (var aa in input)
            {
               var columns = aa.GetType().GetProperties().ToList();
               foreach (var bb in columns)
               {
                  var cccc = bb.GetValue(aa);
                  if(cccc!=null)
                  {
                     bool result = cccc.ToString().Contains(search);
                     if (result)
                     {
                        output.Add(aa);
                        continue;
                      }
                  }

              }

         }
            return output;
    }
  }

Try call method 尝试通话方法

 public GenericList<table1> g = new GenericList<table1>();

 var tabledetails=db.table1.ToList();
 var resultcommonsearch = g.SerachFun(tabledetails, "Dhoni");

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

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