Get specific items from Generic List collection based on condition using C#

Last Reply on Oct 10, 2017 04:39 AM By susanta

Posted on Oct 10, 2017 12:57 AM

i have a list of value i have to grouping it,

i have to grouping it by the values present in the list, the first letter should be C and other are in between 000-199

Ex: C002

C101

C199

C123

like this

i have values a list that contain

D500

C020

C199

C321

i have to collect all the Value start with C and Number in Between 000-199

C001

C003

it will store to one list

how can i do this?

Posted on Oct 10, 2017 04:39 AM
var Taglist = from r in mTagsList.AsEnumerable() where Regex.IsMatch(r, "^[C]+([0-1]{1}[0-9]{1,2})$") select r;

Regular expression @"^[C]+([0-1]{1}[0-9]{1,2})$"


Posted on Oct 10, 2017 01:27 AM Modified on on Oct 10, 2017 01:28 AM

Hi susanta,

Refer below sample code and you will get the filtered records.

List<string> listValues = new List<string>();
listValues.Add("D500");
listValues.Add("C020");
listValues.Add("C199");
listValues.Add("C321");

List<string> filteredList = new List<string>();
foreach (string item in listValues)
{
    string a = item.Substring(0, 1);
    int numbers = Convert.ToInt32(item.Substring(1, item.Length - 1));
    if (a.ToUpper() == "C" && numbers >= 0 && numbers <= 199)
    {
        filteredList.Add(item);
    }
}

Input

D500
C020
C199
C321

OutPut

C020
C199