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

时间:2017-06-07 09:22:27

标签: c# sql linq

我有一张30列的桌子, 它包含1000行。 我想要一个LINQ查询,它检查所有列中的特定值并将结果转换为列表。

例如:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.somedomain\.com$ [NC]
RewriteRule ^/?$ https://somedomain.com [L,R=301]

如何使用一个LINQ查询完成上述操作。非常感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

对于您的请求,所有字段都应该具有相同的类型,至少在静态类型的C#中。

方法Queriable.WhereExpression<Func<T, bool>>谓词作为参数。因此,您需要将谓词o.p1 == val || o.p2 == val || o.p3 = val ...构建为Expression值。此处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;
}

但我们需要为TObj类型为TVal的所有属性动态构建谓词。

为简化代码,我们将构建相等的表达式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);
}

答案 1 :(得分:0)

我得到了答案但是不是完美答案但是工作得很好

   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;
    }
}

创建的通用类对象

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

调用的通用类方法:

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

答案 2 :(得分:0)

使用代码

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

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

尝试调用方法

a = [{"ID83": {"Job1": {"Job_Num": "ABC.179", "Job_PNo": "26", "Job_SNo": "750", "Job_name": "Job1"}}},
{"ID84": {"Job1": {"Job_Num": "ABC.180", "Job_PNo": "27", "Job_SNo": "850", "Job_name": "Job1a"}}},
{"ID85": {"Job2": {"Job_Num": "DEF.20", "Job_PNo": "28", "Job_SNo": "950", "Job_name": "Job2"}}},
{"ID86": {"Job3": {"Job_Num": "CDE.150", "Job_PNo": "29", "Job_SNo": "50", "Job_name": "Job3"}}},
{"ID87": {"Job2": {"Job_Num": "XYZ.20", "Job_PNo": "30", "Job_SNo": "200", "Job_name": "Job2a"}}},
{"ID83": {"Job3": {"Job_Num": "CDE.151", "Job_PNo": "30", "Job_SNo": "55", "Job_name": "Job3a"}}},
]

ret = []
for d in a:
  line = []
  for (k,v) in d.items():
    line.append(k)
    inner_dict = list(v.values())[0]
    line.append(inner_dict['Job_name'])
    line.append(inner_dict['Job_Num'])
    line.append(inner_dict['Job_PNo'])
    line.append(inner_dict['Job_SNo'])
  ret.append(line)

csvstr = '\n'.join([','.join(i) for i in ret])

with file('out.csv', 'w') as f:
    f.write(csvstr)