2

I have list of string and there are number of string in the list. Each string in the list start with number.

List<String> stringList=new List<String>();

stringList.Add("01Pramod");
stringList.Add("02Prakash");
stringList.Add("03Rakhi");
stringList.Add("04Test");
stringList.Add("04Test1");
stringList.Add("04Test2");

I want a Linq query that will return me list of string that starts with 04.

4 Answers 4

9
stringList.Where(s => s.StartsWith("04"))

or

stringList.Where(s => s.StartsWith("04")).ToList()

if you need a list

Sign up to request clarification or add additional context in comments.

Comments

3
var result = stringList.Where(i => i.StartsWith("04"));

Comments

1

Here are the possible solution for it:

 // Lambda
 stringList.FindAll(o => o.StartsWith("04"));

 // LINQ
 (from i in stringList
  where i.StartsWith("04")
  select i).ToList();

Comments

0

I guess this will be easy to understand and its in proper format

var ss=from string g in stringList

where g.Substring(0,2)=="04"

select g;

foreach(string str in ss) { Console.WriteLine(str); }

6 Comments

-1 cause these answer was already (and better) given by Peyton three hours before.
@Oliver: It is not fair. +1 because even though this is not as elegant as others but it will also work. Just because he has another opinion is not the reason to downvote him.
@zerkms: It is not another opinion, it is the same, but using Substring() instead of StartsWith(). So it is the same answer as Peytons, but using another string function. So instead of posting the same answer a second time he should upvote Peytons answer instead and delete his own one.
@Oliver: he is new here, he wants to become the part of community ;-)
@Oliver: btw, even though I was 15 seconds later than Martin Booth my answer was checked, not him :-S
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.