我们如何使用c#对列表进行分组?

时间:2012-09-04 12:20:40

标签: c#

我需要找到失败的测试用例数。这里TC003包含(3个SUB测试用例),其中2个通过,1个失败。

我需要计算总测试用例(4)通过:-(3)失败 - (1)。我怎么能这样做。

TC001-Passed
TC002-Passed
TC003-Passed
TC003-Passed
TC003-Failed
TC004-Passed

我有一个dataTable,即ItemArray[0]包含TestCaseID,ItemArray[3]包含状态

4 个答案:

答案 0 :(得分:1)

好的,这很尴尬,但让我们直截了当:

void Main()
{
    List<string> AllTheTestCasesStrings = new List<string>();
    AllTheTestCasesStrings.Add("TC001 Passed");
    AllTheTestCasesStrings.Add("TC002 Passed");
    AllTheTestCasesStrings.Add("TC003 Passed");
    AllTheTestCasesStrings.Add("TC003 Passed");
    AllTheTestCasesStrings.Add("TC003 Failed");
    AllTheTestCasesStrings.Add("TC004 Passed");

    List<TestCase> AllTheTestCases = new List<TestCase>();

    foreach(string CurrentTest in AllTheTestCasesStrings)
    {
        TestCase NewTest = new TestCase();
        NewTest.Name = CurrentTest.Split(' ')[0];
        NewTest.Passed = CurrentTest.Split(' ')[1].Equals("Passed");

        AllTheTestCases.Add(NewTest);
    }

    int FailedTestCases = AllTheTestCases.Where<TestCase>(x => !x.Passed).GroupBy<TestCase, string>(x => x.Name).Count();
    int PassedTestCases = AllTheTestCases.Where<TestCase>(x => x.Passed).GroupBy<TestCase, string>(x => x.Name).Count();

    Console.WriteLine("Failed: " + FailedTestCases);
    Console.WriteLine("Passed: " + PassedTestCases);
}

public class TestCase
{
    public string Name;
    public bool Passed;
}

答案 1 :(得分:1)

假设您有LINQ可用,您可以尝试这样的解决方案,其中dt是您的数据表,其列索引0为testcase-id(1,2,3,4)和列索引3作为状态(PassedFailed):

var rows = dt.Select();
var results = rows.GroupBy (r => r[0])
    .Select (r => new {
        TestCaseID = r.Key,
        Status = r.Any (x => x[3].Equals("Failed")) ? "Failed" : "Passed"
    } );

var total = results.Count();
var passed = results.Count(r => r.Status == "Passed");
var failed = results.Count (r => r.Status == "Failed");

Console.WriteLine("Total Testcases: " + total);    // 4
Console.WriteLine("Passed: " + passed);            // 3
Console.WriteLine("Failed: " + failed);            // 1

答案 2 :(得分:1)

  

我有一个dataTable,即ItemArray [0]包含TestCaseID和   ItemArray [3]包含状态

好的,这是一种使用DataTable和Linq-To-DataSet的方法:

var tcaseGroups = tbl.AsEnumerable()
    .GroupBy(r => r.Field<String>("TestCaseID"))
    .Select(g => new
    {
        Testcase = g.Key,
        Count = g.Count(),
        CaseInfo = g.GroupBy(r => r.Field<String>("Status"))
                     .Select(gStatus => new
                     {
                         Status = gStatus.Key,
                         Count = gStatus.Count()
                     })
    });

// output the result
foreach (var x in tcaseGroups)
    Console.WriteLine("Testcase:{0}, Count:{1} Results:[{2}]", x.Testcase, x.Count
        , String.Join(",", x.CaseInfo.Select(xx => 
            String.Format("Status:{0} Count:{1}", xx.Status, xx.Count))));

答案 3 :(得分:1)

使用Linq按ItemArray [0]进行分组,然后计算ItemArray [1]的实例。

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication
{
    internal class Program
    {
        private static void Main()
        {
            //table to mimik your structure
            var table = new List<string[]>(new[]
                {
                    new[] {"TC001", null, null, "Passed"},
                    new[] {"TC002", null, null, "Passed"},
                    new[] {"TC003", null, null, "Passed"},
                    new[] {"TC003", null, null, "Passed"},
                    new[] {"TC003", null, null, "Failed"},
                    new[] {"TC004", null, null, "Passed"}
                });

            //calculate
            var grouped = from itemArray in table
                          //group by the testid
                          group itemArray by itemArray[0]
                          into g select new
                              {
                                  TestCaseId = g.Key,
                                  //add up the passes and fails
                                  PassedCount = g.Count(i => i[3] == "Passed"),
                                  FailedCount = g.Count(i => i[3] == "Failed")
                              };

            //enumerate and output to screen
            foreach (var group in grouped)
            {
                Console.WriteLine("TestCase {0} Passed: {1}, Failed: {2}",
                                  group.TestCaseId, group.PassedCount,
                                  group.FailedCount);
            }
            Console.ReadLine();
        }
    }
}