Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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:

C#
Values = "A1,A2"
Var query1 = (from a in tbl1.AsEnumerable()

                     where r.Field<string>("TEMP").Contains(Values)
Posted
Updated 17-May-17 12:42pm
v2

Assuming that tbl1 is a datatable object, you have to combine Where + Any method.

So, take a look at example and change your query to your needs.
C#
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 
 
Share this answer
 
Comments
gani7787 18-May-17 2:13am    
what about the does not contain value like "Fi" and "K"..?
whaere we need to add in the query..?
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;
 
Share this answer
 
v3
Comments
gani7787 18-May-17 2:13am    
Thanks for your result.
My query like below in LINQ without lambda expression.

Tempvalues1 = "S1","S2"; ==> For contain value
Tempvalues2 = "Fi","k"; ==> for doesn't contain value
qry = (from r in tbl1.AsEnumerable()
where r.Field<int>("EmpNo") == Emp && r.Field<string>("Temp").Contains(TempValues1)
&& !(r.Field<string>("Temp").Contains((TempValues2)))
select new
{
}).Count();

how to get the count of the filter recrods..?
Robert Raboud 18-May-17 8:21am    
added the negative search to solution and a variable for count.

You can use Aggregate to cater for multipile target values. Something like.


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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900