Soundbyte
Soundbyte

Reputation: 93

How do I search for a string in a list?

I don't know how I can return a list with all the soundfragments that contain the string in the titel.

    fragmenten = new List<Geluidsfragment>();
    public List<Geluidsfragment> GetFragment(String p)
    {
        fragmenten.IndexOf(p);            
    }

I hope you guys can help me with this problem.

Upvotes: 1

Views: 99

Answers (5)

Mukund
Mukund

Reputation: 1689

LINQ is the best option available with lambda expression.

You can use it like:-

fragmenten.Where(s => s.Title.Contains(title)).ToList()

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460288

You can use List.FindAll:

List<Geluidsfragment> result = fragmenten.FindAll(f => f.Title.Contains(title));

Another option is LINQ's Enumerable.Where with Enumerable.ToList:

result = fragmenten.Where(f => f.Title.Contains(title)).ToList();

Upvotes: 2

yourlist.Where(x => x.property.Contains(value)).ToList();

Upvotes: 0

Sachin
Sachin

Reputation: 40990

Try this

List<Geluidsfragment> requiredList = fragmenten.Where(x => x.Title.Contains(p)).ToList();

Upvotes: 1

Andrei
Andrei

Reputation: 56716

Assuming you have a string Title property:

return fragmenten.Where(f => f.Title.Contains(p)).ToList();

You might need to add this to the using section of your file:

using System.Linq;

Upvotes: 2

Related Questions