none
How to extract only quarter name from List<string> RRS feed

  • Question

  • see my List what kind of data is stored there.

    how to extract only quarter name from List<string>

    so data would be extracted from 2010 FY to 1Q 2014 and rest will be ignored. thanks

                List<string> lst = new List<string>();
                lst.Add("Section");
                lst.Add("Line Item");
                lst.Add("2010 FY");
                lst.Add("2011 FY");
                lst.Add("1Q 2012");
                lst.Add("2Q 2012");
                lst.Add("3Q 2012");
                lst.Add("4Q 2012");
                lst.Add("2012 FY");
                lst.Add("1Q 2013");
                lst.Add("2Q 2013");
                lst.Add("3Q 2013");
                lst.Add("4Q 2013");
                lst.Add("2013 FY");
                lst.Add("1Q 2014");
                lst.Add("Cross Cals1");
                lst.Add("Cross Cals2");
                lst.Add("QC Type");
                lst.Add("QC Check");

    Friday, March 27, 2020 10:50 AM

Answers

  • If you mean get say 1Q, 2Q

    Anonymously

    private void button1_Click(object sender, EventArgs e)
    {
        var lst = new List<string>
        {
            "Section",
            "Line Item",
            "2010 FY",
            "2011 FY",
            "1Q 2012",
            "2Q 2012",
            "3Q 2012",
            "4Q 2012",
            "2012 FY",
            "1Q 2013",
            "2Q 2013",
            "3Q 2013",
            "4Q 2013",
            "2013 FY",
            "1Q 2014",
            "Cross Cals1",
            "Cross Cals2",
            "QC Type",
            "QC Check"
        };
    
        var result = 
            lst.Where(item => char.IsDigit(item[0]) && item[1] is 'Q').Select(item =>
            {
                var parts = item.Split(new char[0]);
                var quarterName = parts[0];
                var year = Convert.ToInt32(parts[1]);
    
                return new { quarterName, year};
            });
    
        foreach (var data in result)
        {
            Console.WriteLine($"{data.quarterName}, {data.year}");
        }
        
    }
     

    Strong typed

    public class Item
    {
        public string QuarterName { get; set; }
        public int Year { get; set; }
    }

    Then

    private void button1_Click(object sender, EventArgs e)
    {
        var lst = new List<string>
        {
            "Section",
            "Line Item",
            "2010 FY",
            "2011 FY",
            "1Q 2012",
            "2Q 2012",
            "3Q 2012",
            "4Q 2012",
            "2012 FY",
            "1Q 2013",
            "2Q 2013",
            "3Q 2013",
            "4Q 2013",
            "2013 FY",
            "1Q 2014",
            "Cross Cals1",
            "Cross Cals2",
            "QC Type",
            "QC Check"
        };
    
        var result = 
            lst.Where(item => char.IsDigit(item[0]) && item[1] is 'Q').Select(item =>
            {
                var parts = item.Split(new char[0]);
                var dataItem = new Item {QuarterName = parts[0], Year = Convert.ToInt32(parts[1])};
                return dataItem;
            });
    
        foreach (var data in result)
        {
            Console.WriteLine($"{data.QuarterName}, {data.Year}");
        }
        
    }

    Result

    1Q, 2012
    2Q, 2012
    3Q, 2012
    4Q, 2012
    1Q, 2013
    2Q, 2013
    3Q, 2013
    4Q, 2013
    1Q, 2014

    Perhaps some ordering

    var result = 
        lst.Where(item => char.IsDigit(item[0]) && item[1] is 'Q').Select(item =>
        {
            var parts = item.Split(new char[0]);
            var dataItem = new Item {QuarterName = parts[0], Year = Convert.ToInt32(parts[1])};
            return dataItem;
        })
            .OrderBy(item => item.QuarterName)
            .ThenBy(item => item.Year);
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange



    Friday, March 27, 2020 2:53 PM
    Moderator
  • If you still like Regular Expressiions, then check this method too:

    List<string> result = lst.Where( i => Regex.IsMatch( i, @"\d\d\d\d FY|[1-4]Q \d\d\d\d" ) ).ToList( );



    • Edited by Viorel_MVP Friday, March 27, 2020 4:28 PM
    • Marked as answer by Sudip_inn Sunday, March 29, 2020 6:04 PM
    Friday, March 27, 2020 4:27 PM

All replies

  • If you mean get say 1Q, 2Q

    Anonymously

    private void button1_Click(object sender, EventArgs e)
    {
        var lst = new List<string>
        {
            "Section",
            "Line Item",
            "2010 FY",
            "2011 FY",
            "1Q 2012",
            "2Q 2012",
            "3Q 2012",
            "4Q 2012",
            "2012 FY",
            "1Q 2013",
            "2Q 2013",
            "3Q 2013",
            "4Q 2013",
            "2013 FY",
            "1Q 2014",
            "Cross Cals1",
            "Cross Cals2",
            "QC Type",
            "QC Check"
        };
    
        var result = 
            lst.Where(item => char.IsDigit(item[0]) && item[1] is 'Q').Select(item =>
            {
                var parts = item.Split(new char[0]);
                var quarterName = parts[0];
                var year = Convert.ToInt32(parts[1]);
    
                return new { quarterName, year};
            });
    
        foreach (var data in result)
        {
            Console.WriteLine($"{data.quarterName}, {data.year}");
        }
        
    }
     

    Strong typed

    public class Item
    {
        public string QuarterName { get; set; }
        public int Year { get; set; }
    }

    Then

    private void button1_Click(object sender, EventArgs e)
    {
        var lst = new List<string>
        {
            "Section",
            "Line Item",
            "2010 FY",
            "2011 FY",
            "1Q 2012",
            "2Q 2012",
            "3Q 2012",
            "4Q 2012",
            "2012 FY",
            "1Q 2013",
            "2Q 2013",
            "3Q 2013",
            "4Q 2013",
            "2013 FY",
            "1Q 2014",
            "Cross Cals1",
            "Cross Cals2",
            "QC Type",
            "QC Check"
        };
    
        var result = 
            lst.Where(item => char.IsDigit(item[0]) && item[1] is 'Q').Select(item =>
            {
                var parts = item.Split(new char[0]);
                var dataItem = new Item {QuarterName = parts[0], Year = Convert.ToInt32(parts[1])};
                return dataItem;
            });
    
        foreach (var data in result)
        {
            Console.WriteLine($"{data.QuarterName}, {data.Year}");
        }
        
    }

    Result

    1Q, 2012
    2Q, 2012
    3Q, 2012
    4Q, 2012
    1Q, 2013
    2Q, 2013
    3Q, 2013
    4Q, 2013
    1Q, 2014

    Perhaps some ordering

    var result = 
        lst.Where(item => char.IsDigit(item[0]) && item[1] is 'Q').Select(item =>
        {
            var parts = item.Split(new char[0]);
            var dataItem = new Item {QuarterName = parts[0], Year = Convert.ToInt32(parts[1])};
            return dataItem;
        })
            .OrderBy(item => item.QuarterName)
            .ThenBy(item => item.Year);
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange



    Friday, March 27, 2020 2:53 PM
    Moderator
  • From the top of my head - not tested

    var quater1Items = lst.Where(x=>x.StartsWith("1Q")).OrderBy(x=>x.Substring(x.Length - 4)).ToList();


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles


    • Edited by Naomi NModerator Friday, March 27, 2020 5:15 PM
    • Marked as answer by Sudip_inn Saturday, March 28, 2020 5:01 PM
    • Unmarked as answer by Sudip_inn Sunday, March 29, 2020 6:00 PM
    Friday, March 27, 2020 3:13 PM
    Moderator
  • If you still like Regular Expressiions, then check this method too:

    List<string> result = lst.Where( i => Regex.IsMatch( i, @"\d\d\d\d FY|[1-4]Q \d\d\d\d" ) ).ToList( );



    • Edited by Viorel_MVP Friday, March 27, 2020 4:28 PM
    • Marked as answer by Sudip_inn Sunday, March 29, 2020 6:04 PM
    Friday, March 27, 2020 4:27 PM
  • Only Q & FY value should come....does the regex do it ?
    Saturday, March 28, 2020 5:01 PM
  • I want to display data like 1Q, 2Q,3Q,4Q & FY.....your code giving data only with Q.

    Sunday, March 29, 2020 6:00 PM
  • I want to display data like 1Q, 2Q,3Q,4Q & FY. so your code will not produce right answer.
    Sunday, March 29, 2020 6:02 PM
  • Thanks. Best answer you have given.
    Sunday, March 29, 2020 6:04 PM
  • My code was showing an example for the 1Q. 

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    Sunday, March 29, 2020 6:10 PM
    Moderator
  • i test your code but does not work

    var lst = new List<string>
                {
                    "Section",
                    "Line Item",
                    "2010 FY",
                    "2011 FY",
                    "1Q 2012",
                    "2Q 2012",
                    "3Q 2012",
                    "4Q 2012",
                    "2012 FY",
                    "1Q 2013",
                    "2Q 2013",
                    "3Q 2013",
                    "4Q 2013",
                    "2013 FY",
                    "1Q 2014",
                    "Cross Cals1",
                    "Cross Cals2",
                    "QC Type",
                    "QC Check"
                };
                var quater1Items = lst.Where(x => x.StartsWith("1Q")).OrderBy(x => x.Substring(x.Length - 4)).ToList();

    Quater order should be like

    2010 FY

    2011 FY

    1Q 2012

    2Q 2012

    3Q 2012

    4Q 2012

    2012 FY

    1Q 2013

    so on....

    thanks

    Wednesday, May 6, 2020 5:28 PM
  • NO your code could not order quater

    private void button2_Click(object sender, EventArgs e)
            {
                var lst = new List<string>
                {
                    "Section",
                    "Line Item",
                    "1Q 2013",
                    "2011 FY",
                    "1Q 2012",
                    "2Q 2012",
                    "3Q 2012",
                    "4Q 2012",
                    "2012 FY",
                    "2Q 2013",
                    "3Q 2013",
                    "4Q 2013",
                    "2013 FY",
                    "1Q 2014",
                    "2010 FY",
                    "Cross Cals1",
                    "Cross Cals2",
                    "QC Type",
                    "QC Check"
                };
                List<string> result = lst.Where(i => Regex.IsMatch(i, @"\d\d\d\d FY|[1-4]Q \d\d\d\d")).ToList();
    
            }

    you can understand just run the code.

    Wednesday, May 6, 2020 5:32 PM