YogeshWaran
YogeshWaran

Reputation: 2281

How to find substring in list of strings using LINQ

i need to find out a string from collection of strings using its sub string. This sub string must be in starting.

Upvotes: 1

Views: 6454

Answers (2)

Vetrivel mp
Vetrivel mp

Reputation: 1224

you can do something like this,

List<string> collection = new List<string>();
        collection.Add("example sample");
        collection.Add("sample");
        collection.Add("example");

        var varSubstring = collection.Where(x => x.IndexOf("sample")==0).ToList();
        foreach (var vartemp in varSubstring)
        {
        }

Upvotes: 2

SLaks
SLaks

Reputation: 887449

collection.FirstOrDefault(s => s.StartsWith(whatever))

Upvotes: 10

Related Questions