如何在linq查询中使用like运算符

如何在linq查询中使用like运算符

本文介绍了如何在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)

推荐答案

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;


var query = dt.AsEnumerable();
           var strs = new[] { "S1", "S2" };
           query = strs.Aggregate(query, (current, s) => current.Where(x => x.Field<string>("Temp").Contains(s)));


这篇关于如何在linq查询中使用like运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 00:42