Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I use code and work fine:
C#
var KooperacijaRN = this.radni_nalozi_2018DataGridView.Rows.Cast<datagridviewrow>().Where(row => row.Cells["R.b. RN"].Value.ToString() == "KO");  
            var childCount4 = KooperacijaRN.Count();  
            this.BlokiraniRNLabel.Text = childCount4.ToString();

Need to use word %KO% in query

What I have tried:

I need

var KooperacijaRN = this.radni_nalozi_2018DataGridView.Rows.Cast<DataGridViewRow>().Where(row => row.Cells["R.b. RN"].Value.ToString() LIKE "%KO%"); 

But this dont work
Please help
Thank you
Posted
Updated 19-May-21 23:13pm
v2
Comments
CHill60 20-May-21 4:54am    
"But this dont work" - does it produce anything or does it throw an exception?
BillWoodruff 20-May-21 5:04am    
describe what happens and any error messages clearly
PIEBALDconsult 20-May-21 10:07am    
What value are you working with?

You can use Contains() for your needs

C#
var KooperacijaRN = this.radni_nalozi_2018DataGridView.Rows.Cast<DataGridViewRow>().Where(row => row.Cells["R.b. RN"].Value.ToString().ToUpper().Contains("KO")); 
 
Share this answer
 
v3
Comments
Goran Bibic 20-May-21 5:19am    
I try, but no error, dont work
It looks like you're not querying against the database but against an existing dataset. For this reason you can't use the LIKE as if you had run it directly on SQL, but you can use an appropriate C# version instead. For example:

C#
Rows
  .Cast<DataGridViewRow>()
  .Where(row => row.Cells["R.b. RN"].Value.ToString().ToUpper().Contains("KO"));

This essentially replicates the behaviour you're expecting from the LIKE keyword. If you want to do this from the SQL then you'll need to amend your SQL query to leverage it.
 
Share this answer
 
Comments
Goran Bibic 20-May-21 5:19am    
I try, but no error, dont work
Chris Copeland 20-May-21 5:34am    
What part isn't working? Are you not seeing the count in the textbox, or is the code just not running? Have you tried putting a breakpoint in the code to step through line-by-line and see where you're not getting the results you want?
Goran Bibic 20-May-21 7:02am    
not seeing the count in the textbox...code no errors
Chris Copeland 20-May-21 8:02am    
Did you do as I asked and put a breakpoint in the code to see what happens when it runs? Sounds like the code isn't running at all, which could indicate it's not being triggered for some reason. Unfortunately we can't see the rest of your code, nor do we understand how the code should be triggering, so we can't provide that 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