none
C# code snippet to process WorkItemCollection RRS feed

  • Question

  • Hi Team,

    I have below WorkItemQuery that will return resuts that can be stored to WorkItemCollection.

    WorkItemCollection details = Bug_state_query.RunQuery();

    WorkItemCollection will have below details

    ID Link  Work Item Type
    28482  Bug
    64428 Parent Test Case
    66138  Bug
    18466 Related User Story
    64934 Tested  Test Case
    73025  Bug
    4094 Parent User Story
    74518  Bug
    9148 Parent User Story
    11843 Tests Test Case

    I need to
    a. Read above WorkItemCollection using C#, A Bug can have linked TestCase or linked UserStory or Both(UserStory & TestCase)
    b. Create and write data to two DataTables by parsing above Collection
       BUG_UserStory(DataTable1)[Contains Bugs that has UserStories(It is ok if it also contains TestCase]
       EX: Output
       BugID, UserStoryId
       66138, 18466
       73025, 4094
       74518, 9148

       BUG_TestCase(DataTable2)[Contains Bugs that has only TestCase no UserStory]
       EX: Output
       BugID, TestCaseId
       28482, 64428
       9148,11843
    c. These two tables are mutually exclusive, DataTable1 and DataTable2 should be unique no similar record should be present in both the tables.  

    Wednesday, November 29, 2017 5:44 AM

Answers

  • Hello Anil,

    Try the below code.

     private void button1_Click(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[] { new DataColumn("Id"), new DataColumn("Link"), new DataColumn("Work Item Type") });
                dt.Rows.Add("28482", null, "Bug");
                dt.Rows.Add("64428", "Parent", "Test Case");
                dt.Rows.Add("66138", null, "Bug");
                dt.Rows.Add("18466", "Related", "User Story");
                dt.Rows.Add("64934", "Tested", "Test Case");
                dt.Rows.Add("73025", null, "Bug");
                dt.Rows.Add("73025", null, "ssss");
                dt.Rows.Add("4094", "Parent", "User Story");
                dt.Rows.Add("74518", null, "Bug");
                dt.Rows.Add("9148", "Parent", "User Story");
                dt.Rows.Add("11843", "Tests", "Case");
                dataGridView1.DataSource = dt;
    
                //For your a requirement
                var ss1 = dt.AsEnumerable().Where(x => new string[] { "Bug", "Test Case", "User Story" }.Contains(x.Field<string>("Work Item Type"))).ToList();
    
                var ss2 = ss1.Select(x => x.Field<string>("Work Item Type") == "Bug" ? ss1.IndexOf(x) : -1).Where(w=>w!=-1).ToArray();
    
                DataTable Bug_TestCase = new DataTable("BugTestCase");
                Bug_TestCase.Columns.AddRange(new DataColumn[] {new DataColumn("BugId"),new DataColumn("TestCaseID")});
    
                DataTable Bug_User = Bug_TestCase.Clone();
                Bug_User.TableName = "BugUser";
                Bug_User.Columns["TestCaseId"].ColumnName = "UserStoryId";
    
    
                for (int i = 0; i < ss2.Count()-1; i++) {
                    GetDataTables(ss2[i], ss2[i + 1], ss1,ref Bug_TestCase,ref Bug_User);
                }
    
                if (ss2[ss2.Length - 1] < ss1.Count())
                    GetDataTables(ss2[ss2.Length-1], ss1.Count, ss1, ref Bug_TestCase, ref Bug_User);
    
                dataGridView2.DataSource = Bug_TestCase;
                dataGridView3.DataSource = Bug_User;
            }
    
            public void GetDataTables(int begin, int end, List<DataRow> sourceDt,ref DataTable Bug_TestCase, ref DataTable Bug_User) {
                IEnumerable<DataRow> list=sourceDt.Skip(begin).Take(end - begin);
                if (!list.Any(x => x.Field<string>("Work Item Type") == "User Story"))
                {
                    Bug_TestCase.Rows.Add(sourceDt[begin].Field<string>("Id"), sourceDt[begin + 1].Field<string>("Id"));
                }
                else {
                    Bug_User.Rows.Add(sourceDt[begin].Field<string>("Id"), list.Where(x => x.Field<string>("Work Item Type") == "User Story").FirstOrDefault().Field<string>("Id"));
                }
            }
    
        }


    Best regards,

    Neil Hu


    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.

    Thursday, November 30, 2017 12:03 PM
    Moderator
  • This looks a bit like TFS Data Tables, if it is, that would be an important information. It is also likely that you can use the TFS API or WebAccess for it.

    Please be so kind to close your Threads when you found an answer, these Threads should help everyone with similar issues.
    You can close a Thread via the"Mark as Answer" link below posts. You can mark your own posts as answers if you were not helped out but found a solution, in such a case, please provide the answer.
    Happy coding
    PS: I assure everyone that I did not ever had the desire to offend anyone.


    Thursday, November 30, 2017 12:50 PM

All replies

  • Hello Anil,

    I'm confused with your table structure. I can't get some connection between the collection and output. I found some rows in your collections that don't have "Bug" value but your expecting output still indicates the row have "Bug" value. Like the row which id is 9148, and what difference between the row 28482 and  row 66138, row 73025, row 74518. They look similar on my side. You could add some explains with you requirement so that I could provide more exact support for you.

    Best regards,

    Neil Hu


    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.

    Wednesday, November 29, 2017 7:49 AM
    Moderator
  • Sorry for Confusion. My Bad, below is the sample

    ID          Link    Work Item Type
    28482                 Bug
    64428    Parent   Test Case
    66138                 Bug
    18466   Related    User Story
    64934   Tested     Test Case

    Note: there is not corresponding value for WorkItemType "Bug",

    Also, please ignore collection, just treat above is Sample is stored in DataTable and

    I need to
    a. Read above DataTable using C#, A Bug can have linked TestCase or linked UserStory or Both(UserStory & TestCase)
    b. Create and write data to two DataTables by parsing above Collection
       BUG_UserStory(DataTable1)[Contains Bugs that has UserStories(It is ok if it also contains TestCase]
       EX: Output
       BugID, UserStoryId
       66138, 18466
       73025, 4094
       74518, 9148

       BUG_TestCase(DataTable2)[Contains Bugs that has only TestCase no UserStory]
       EX: Output
       BugID, TestCaseId
       28482, 64428
       9148,11843
    c. These two tables are mutually exclusive, DataTable1 and DataTable2 should be unique no similar record should be present in both the tables.  

    Wednesday, November 29, 2017 12:08 PM
  • Hello Anil,

    Try the below code.

     private void button1_Click(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[] { new DataColumn("Id"), new DataColumn("Link"), new DataColumn("Work Item Type") });
                dt.Rows.Add("28482", null, "Bug");
                dt.Rows.Add("64428", "Parent", "Test Case");
                dt.Rows.Add("66138", null, "Bug");
                dt.Rows.Add("18466", "Related", "User Story");
                dt.Rows.Add("64934", "Tested", "Test Case");
                dt.Rows.Add("73025", null, "Bug");
                dt.Rows.Add("73025", null, "ssss");
                dt.Rows.Add("4094", "Parent", "User Story");
                dt.Rows.Add("74518", null, "Bug");
                dt.Rows.Add("9148", "Parent", "User Story");
                dt.Rows.Add("11843", "Tests", "Case");
                dataGridView1.DataSource = dt;
    
                //For your a requirement
                var ss1 = dt.AsEnumerable().Where(x => new string[] { "Bug", "Test Case", "User Story" }.Contains(x.Field<string>("Work Item Type"))).ToList();
    
                var ss2 = ss1.Select(x => x.Field<string>("Work Item Type") == "Bug" ? ss1.IndexOf(x) : -1).Where(w=>w!=-1).ToArray();
    
                DataTable Bug_TestCase = new DataTable("BugTestCase");
                Bug_TestCase.Columns.AddRange(new DataColumn[] {new DataColumn("BugId"),new DataColumn("TestCaseID")});
    
                DataTable Bug_User = Bug_TestCase.Clone();
                Bug_User.TableName = "BugUser";
                Bug_User.Columns["TestCaseId"].ColumnName = "UserStoryId";
    
    
                for (int i = 0; i < ss2.Count()-1; i++) {
                    GetDataTables(ss2[i], ss2[i + 1], ss1,ref Bug_TestCase,ref Bug_User);
                }
    
                if (ss2[ss2.Length - 1] < ss1.Count())
                    GetDataTables(ss2[ss2.Length-1], ss1.Count, ss1, ref Bug_TestCase, ref Bug_User);
    
                dataGridView2.DataSource = Bug_TestCase;
                dataGridView3.DataSource = Bug_User;
            }
    
            public void GetDataTables(int begin, int end, List<DataRow> sourceDt,ref DataTable Bug_TestCase, ref DataTable Bug_User) {
                IEnumerable<DataRow> list=sourceDt.Skip(begin).Take(end - begin);
                if (!list.Any(x => x.Field<string>("Work Item Type") == "User Story"))
                {
                    Bug_TestCase.Rows.Add(sourceDt[begin].Field<string>("Id"), sourceDt[begin + 1].Field<string>("Id"));
                }
                else {
                    Bug_User.Rows.Add(sourceDt[begin].Field<string>("Id"), list.Where(x => x.Field<string>("Work Item Type") == "User Story").FirstOrDefault().Field<string>("Id"));
                }
            }
    
        }


    Best regards,

    Neil Hu


    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.

    Thursday, November 30, 2017 12:03 PM
    Moderator
  • This looks a bit like TFS Data Tables, if it is, that would be an important information. It is also likely that you can use the TFS API or WebAccess for it.

    Please be so kind to close your Threads when you found an answer, these Threads should help everyone with similar issues.
    You can close a Thread via the"Mark as Answer" link below posts. You can mark your own posts as answers if you were not helped out but found a solution, in such a case, please provide the answer.
    Happy coding
    PS: I assure everyone that I did not ever had the desire to offend anyone.


    Thursday, November 30, 2017 12:50 PM