Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am having a string array and i am getting 10 strings into it and i want to find a specific string and based on that i have to do something.But how to query a string array??
Posted
Comments
[no name] 10-Jul-14 7:47am    
Fairly easy to do using LINQ.

 
Share this answer
 
Comments
chandra sekhar 10-Jul-14 7:50am    
I have already checked that link but it dint worked.I cant get Findall.
You could use LINQ to search on it. Like in the next example:
C#
var query = from item in yourStringArray
             where item.Contains("ana") 
             select item; // First you should create the LINQ query;
string firstResult = query.FirstOrDefault(); //Then get the first result from the query!
 
Share this answer
 
Comments
chandra sekhar 10-Jul-14 8:04am    
When i am using your query the firstresult value is true for every string.
Raul Iloc 11-Jul-14 2:12am    
I just gave you an example about how to search in the array by using LINQ, and you could modify it as you want, for example to get a list of results not only the first one, bu using "ToList()".
C#
string [] arr = {"Rahul","Ricky","Pravu"};
    var target = "Pravu";
    var results = Array.FindAll(arr, s => s.Equals(target));


It's a sample for finding string from array.
 
Share this answer
 
You can use linq lambda expression

C#
using System.Linq;

            string[] array = { "one", "two" };
//return IEnumerable
            array.Where(o => o == "two");


or as someone stated before via link:

C#
string [] arr = {"One","Two","Three"};
       var target = "One";
       var results = Array.FindAll(arr, s => s.Equals(target));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900