如何在linq查询中使用like运算符
问题描述:
如何在linq中使用like运算符。我有下表。
i只想过滤并获得如下条件的记录
whereTemp contains( S1,S2)==>我想过滤整列中的所有S1和S2。
我也想要在哪里Temp不在里面( %Fi%,%K%)记录。
我们如何写LINQ查询。
下面的样本表。
Hi,
How to use like operator in linq. i have the below table.
i just want to filter and get the records like below condition
"where "Temp contains" ("S1,S2") ==> i want to filter all S1 and S2 in entire columns.
also i want to eleminate "where Temp not in like (%Fi%,%K%) records.
How do we write in LINQ Query.
sample table below.
Emp Temp
100 S1+S2+S3
100 S1+S2+S3
100 S1+S2+S3
100 S1+S2+S3
100 S1+S2+S3
100 Z1+Z2+Z3
100 Z1+Z2+Z3
100 Z1+Z2+Z3
100 Z1+Z2+Z3
100 Z1+Z2+Z3
100 Z1+Z2+Z3
100 Z1+Z2+Z3
100 Z1+Z2+Z3
100 Z1+Z2+Z3
100 S1+Z3
100 L1+L2
100 L1+L2
100 A+B
100 C+DD
100 EE+Fin
100 Ke+W
我尝试过:
What I have tried:
Values = "A1,A2"
Var query1 = (from a in tbl1.AsEnumerable()
where r.Field<string>("TEMP").Contains(Values)
答
假设tbl1
是一个数据表对象,你必须结合使用其中
+任何
方法。
所以,拿查看示例并根据需要更改查询。
Assuming thattbl1
is a datatable object, you have to combineWhere
+Any
method.
So, take a look at example and change your query to your needs.
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Emp", typeof(int)));
dt.Columns.Add(new DataColumn("Temp", typeof(string)));
dt.Rows.Add(new object[]{100, "S1+S2+S3"});
dt.Rows.Add(new object[]{100, "S1+S2+S3"});
dt.Rows.Add(new object[]{100, "S1+S2+S3"});
dt.Rows.Add(new object[]{100, "S1+S2+S3"});
dt.Rows.Add(new object[]{100, "S1+S2+S3"});
dt.Rows.Add(new object[]{100, "Z1+Z2+Z3"});
dt.Rows.Add(new object[]{100, "Z1+Z2+Z3"});
dt.Rows.Add(new object[]{100, "Z1+Z2+Z3"});
dt.Rows.Add(new object[]{100, "Z1+Z2+Z3"});
dt.Rows.Add(new object[]{100, "Z1+Z2+Z3"});
dt.Rows.Add(new object[]{100, "Z1+Z2+Z3"});
dt.Rows.Add(new object[]{100, "Z1+Z2+Z3"});
dt.Rows.Add(new object[]{100, "Z1+Z2+Z3"});
dt.Rows.Add(new object[]{100, "Z1+Z2+Z3"});
dt.Rows.Add(new object[]{100, "S1+Z3"});
dt.Rows.Add(new object[]{100, "L1+L2"});
dt.Rows.Add(new object[]{100, "L1+L2"});
dt.Rows.Add(new object[]{100, "A+B"});
dt.Rows.Add(new object[]{100, "C+DD"});
dt.Rows.Add(new object[]{100, "EE+Fin"});
dt.Rows.Add(new object[]{100, "Ke+W"});
string[] values = new string[]{"S1", "S2"};
var qry = dt.AsEnumerable()
.Where(x=> x.Field<string>("Temp")
.Split(new string[]{"+"}, StringSplitOptions.RemoveEmptyEntries)
.Any(y=>values.Contains(y)));
结果:
Result:
Emp Temp
100 S1+S2+S3
100 S1+S2+S3
100 S1+S2+S3
100 S1+S2+S3
100 S1+S2+S3
100 S1+Z3
string[] values = new string[] { "S1", "S2" };
string[] notval = new string[] { "Fi", "K" } ;
var qry = tbl1.AsEnumerable()
.Where(x => (x.Field<string>("Temp").Contains(values[0]) ||
x.Field<string ("Temp").Contains(values[1]) &&
(!x.Field<string>("Temp").Contains(notval[0]) ||
!x.Field<string ("Temp").Contains(notval[1])));
int count = qry.Count;
您可以使用
Aggregate
来满足多目标值。例如。
You can use
Aggregate
to cater for multipile target values. Something like.
var query = dt.AsEnumerable();
var strs = new[] { "S1", "S2" };
query = strs.Aggregate(query, (current, s) => current.Where(x => x.Field<string>("Temp").Contains(s)));