-1

I am having list using the following item below

Id CategoryName

1  CinemaGallery,CinemaEvents,Cinema

2  CinemaNews, Cinema, CinemaTrailer

3  TamilCinemaNews, TamilCinema, Tamil

Now i have to split each categoryname and have to find the list that contains each category. For example I have to find the list that has Categoryname only Cinema or only CinemaEvents or only TamilCinema.

I used Contains method but it returns all the list which has Cinema in the each string.

Could you please help me?

Ria
  • 10,237
  • 3
  • 33
  • 60

5 Answers5

1

Then you just have to use == instead of IndexOf or Contains:

var cinema = list.First(i => i.Name == "Cinema");

or using List.Find:

var cinema = list.Find(i => i.Name == "Cinema");

You can use Enumerable.Where or List.FindAll to find all matching elements:

var cinemas = list.FindAll(i => i.Name == "Cinema");
cinemas = list.Where(i => i.Name == "Cinema").ToList();

Edit (to take your last edit into account):

var allCinemasOnly = list
        .Where(l => l.All(cat => cat.CategoryName == "Cinema"))
        .ToList();
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

Try something like this:

foreach (string s in myList)
{
    if (s.Contains("Cinema") && !s.Contains("Cinema "))
    {
        //do comething with 's'
    }
}
kdmurray
  • 2,988
  • 3
  • 32
  • 47
0

Try this:

List<string> yourNewList = yourList.Select(x => x = "Cinema").ToList();
Habibillah
  • 27,347
  • 5
  • 36
  • 56
0
You can do like this ..

var itm = list.Find(a =>
        {
            var s = a.Split(Convert.ToChar(",")).ToList();
            return s.Exists(b => b == "Cinema");
        });
Neeraj Kumar Gupta
  • 2,157
  • 7
  • 30
  • 58
0

try this

var list = yourlist.Where(a => a == "Cinema").ToArray();
Usman
  • 3,200
  • 3
  • 28
  • 47