I have declared following List of strings-
List lstfruits= new List(){
{ "apple-2015.05.18.html" },{ "apple-1997.01.11.html" },{ "mango-2013.02.12.html" },{ "peach-2015.04.12.html" },{ "peach-1998.11.12.html" },{ "other-2013.02.12.html" },
};
I need to match all records which contain 'apple'. Below are the results that I want-
"apple-2015.05.18.html"
"apple-1997.01.11.html"
I wrote below , but it returns zero results-
var items = lstfruits.Where(pp => pp.Substring(0, pp.IndexOf('-')) == "apple");
Please help where I am wrong.
T KPosted Jun 17, 2015, 4:44 AM
Upendra Pratap ShahiPosted Jun 17, 2015, 2:37 AM
Saroj Kumar SahuPosted Jun 17, 2015, 1:17 AM
Sreekanth ReddyPosted Jun 17, 2015, 1:12 AM
Bhushan BhasmePosted Jun 17, 2015, 1:08 AM
List
{ "apple-2015.05.18.html" },
{ "apple123-1997.01.11.html" },
{ "apple-1997.01.11.html" },
{ "mango-2013.02.12.html" },
{ "peach-2015.04.12.html" },
{ "peach-1998.11.12.html" },
{ "other-2013.02.12.html" },
};
string a = "apple";
lstfruits = lstfruits.Where(m => m.Contains(a)).ToList();
foreach (var item in lstfruits)
{
if (item.Split('-')[0].Length == a.Length)
{
lstfruitName.Add(item);
}
}
lstfruits = lstfruits.Where(m => m.Contains(a)).ToList();
foreach (var item in lstfruits)
{
if(item.Substring(0,item.IndexOf('-'))=="apple")
{
lstfruitName.Add(item);
}
}
}
Hope This Will Help You
T KPosted Jun 17, 2015, 1:04 AM
Saroj Kumar SahuPosted Jun 17, 2015, 1:03 AM
Sreekanth ReddyPosted Jun 17, 2015, 1:01 AM