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.
list.Where(x => x.ToString().ToLower().Split(',').Where(a => a.Trim() == "a").Any()).ToList();
编辑:将Count()> 0更改为Any()以获得更好的性能
将其转换为字符串数组,并在数组中查找字符串。
list = list.Where(x => x.Content.ToLower().Split(',').IndexOf("a")>= 0).ToList();
Try this:
IList<ContentDomain> returned = new List<ContentDomain>();
foreach(ContentDomain myList in list)
{
var ret = myList.Content.Split(',');
bool exists = (from val in ret
where val.Contains('a')
select true).FirstOrDefault();
if (exists)
returned.Add(myList);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.