none
c# Robust Ling Idea RRS feed

  • Question

  • I am trying to come up a link to process or get 3 messages at a time in the color table below.  In the color table below I 8 records. There will be 3 gets, the last get will only 2 records. So we want to account for the remaining records. Is there a linq that can handle this logic I am trying to achieve. Once I get the record in batches of 3's I go do something with the batch

    Color Table:

    ID  |   Item

    1    |    red

    2    |    blue

    3    |    green

    4    |    brown

    5    |   purple

    6    |   yellow

    7    |   orange

    8    |   violet


    pianoboyCoder

    Thursday, October 26, 2017 1:47 AM

All replies

  • Hi pianoboyCoder,

    Thank you for posting here.

    According to your description, I am not sure where you store the data. I use dataTable for example. Please try the following code.

     static void Main(string[] args)
            {
                List<string> item = new List<string>();
                DataTable table = GetTable();
                foreach (DataRow row in table.Rows)
                {
                    item.Add(row["Item"].ToString());
                }
                var q = item.Select((v, i) => new { v, t = i / 3 }).GroupBy(p => p.t, p => p.v);
                var result = q.Select(g => g.ToList()).ToList();//here is the list you want.
            }
            static DataTable GetTable()
            {
                DataTable table = new DataTable(); // New data table.
                table.Columns.Add("ID", typeof(int));
                table.Columns.Add("Item", typeof(string));
    
                table.Rows.Add(1, "red");
                table.Rows.Add(2, "blue");
                table.Rows.Add(3, "green");
                table.Rows.Add(4, "brown");
                table.Rows.Add(5, "purple");
                table.Rows.Add(6, "yellow");
                table.Rows.Add(7, "yellow");
                table.Rows.Add(8, "violet");
                return table; // Return reference.
            }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 27, 2017 6:11 AM
    Moderator
  • Hi painoboyCoder,

    Any update for your question? If your question has been solved, please mark the solution as answer to close the thread. This will make answer searching easier in the forum and be beneficial to community members as well.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 30, 2017 2:35 AM
    Moderator