Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List Collection Contains exact string

Tags:

c#

list

generics

I have a List Collection and say that i am adding 3 items to them.

list.Add(new ContentDomain() {  Id = "1" , Content = "aaa,bbb,ccc,ddd"});
list.Add(new ContentDomain() {  Id = "2" , Content = "aa,bb,cc,dd"});
list.Add(new ContentDomain() {  Id = "3" , Content = "a,b,c,d"});

Now what i want is to fetch the rows that have just 'a' in the Content attribute.

Like i tried something like

list = list.Where(x => x.Content.ToLower().Contains("a")).ToList();

but that would give me all the three rows.

i want to search in a string for the exact string only.

like image 299
Manek Avatar asked Feb 01 '26 05:02

Manek


1 Answers

list.Where(x => x.ToString().ToLower().Split(',').Where(a => a.Trim() == "a").Any()).ToList();

edit: Changed Count() > 0 to Any() for better performance

like image 148
Peter Avatar answered Feb 03 '26 18:02

Peter