LINQ查询帮助-第一个查询结果上的第二个查询

sqyvllje  于 2023-05-04  发布在  其他
关注(0)|答案(1)|浏览(172)

我有一个产品测试结果表,MfgTests。测试失败的原因有很多,但我感兴趣的是这个。

  • 总体通过的已运行测试总数
  • 门开关失败且从未通过的测试次数
  • 门开关第一次出现故障,但随后通过的测试次数

每个记录都有唯一的UID和日期/时间条目。该产品可以多次测试。
我目前掌握的情况:

var totalPass = (from d in KAKs
                where d.TestResult == "PASS"
                select d);
Console.WriteLine("Total Passing: \t" + totalPass.Count());

var dataPass = (from d in KAKs
                where d.DoorSwitch == "PASS"
                select d);
                Console.WriteLine("Total Door Switch Passes: \t" + dataPass.Count());
var dataFail = (from d in KAKs
                where d.DoorSwitch == "FAIL"
                select d);
                Console.WriteLine("Total Door Switch Fails: \t" + dataFail.Count());

foreach (var uid in dataFail)
{
    Console.WriteLine(uid.UID);
    var distinctPass =
        from list in dataPass
        group list by list.UID into grouped
        where grouped.Count() == 1
        select grouped;
        Console.WriteLine("Door Switch Fail but pass later: \t" + distinctPass.Count());

这给了我前两个项目,但我有一个困难的时间与最后一个。
我创建了一个测试表来说明。

这是回归:

  • 总通过次数:6
  • 门开关通过总数:7
  • 门开关故障总数:2
  • 门开关失效,但随后通过:4〈-但是对于UID 10,这应该是1个结果

我以为我拿到了。这对我的测试数据有效,但对我的真实的数据无效。

var distinctPass =
    from list in dataPass
    group list by list.UID into grouped
    where grouped.Count() != 1
    select grouped;
Console.WriteLine("Door Switch Fail but pass later: \t" + distinctPass.Count());
z0qdvdin

z0qdvdin1#

这是我的解决方案,希望它能帮助未来的人。

static IEnumerable<T> CreateEmptyEnumerable<T>(IEnumerable<T> templateQuery)
{
    return Enumerable.Empty<T>();
}

var totalPass = (from d in KAKs
                 where d.TestResult == "PASS"
                 select d);
Console.WriteLine("Total Passing: \t" + totalPass.Count());

var dataPass = (from d in KAKs
                where d.DoorSwitch == "PASS"
                select d);
Console.WriteLine("Total Door Switch Passes: \t" + dataPass.Count());

var failUIDs = (from d in KAKs
                where d.DoorSwitch == "FAIL"
                select d);
Console.WriteLine("Total Door Switch Fails: \t" + failUIDs.Count());

var finalResults = CreateEmptyEnumerable(failUIDs);
var finalPass = CreateEmptyEnumerable(failUIDs);
var finalFail = CreateEmptyEnumerable(failUIDs);

var passfailCount = 0;
var passCount = 0;
var failCount = 0;

DumpContainer dc = new();

foreach (var item in failUIDs)
{
    finalPass = (from d in KAKs
                 where d.DoorSwitch == "PASS"
                 where d.UID == item.UID
                 select d);
    finalFail = (from d in KAKs
                 where d.DoorSwitch == "FAIL"
                 where d.UID == item.UID
                 select d);
                 
    finalResults = finalPass;
    finalResults = finalPass.Union(finalFail);
    
    dc.AppendContent(finalResults);
    finalResults.Count();
    if (finalPass.Count() != 0 && finalFail.Count() != 0)
    {
        passfailCount++;
    }
    else if (finalPass.Count() == 0 && finalFail.Count() != 0)
    {
        failCount++;
    }
    else if (finalPass.Count() != 0 && finalFail.Count() == 0)
    {
        passCount++;
    }
}
Console.WriteLine("Pass/Fail: " + passfailCount);
Console.WriteLine("Pass: " + passCount);
Console.WriteLine("Fail: " + failCount);
//dc.Dump();

相关问题