使用嵌入式列表进行Linq to SQL分组会导致查询过多

时间:2016-09-30 22:29:14

标签: c# sql linq

我正在开发一个查询来抓取并加入C#中的一些SQL表,并且在数据集中的分组和枚举方面遇到了一些麻烦。我的查询如下。这给我提供了我正在寻找的格式的数据,但是当我尝试添加如下所示的枚举列表时,它会花费太长时间。当我看到底层时,我可以看到它正在执行太多的SQL查询。我想把它变成一个。使用LinqPad:

import java.util.Scanner;

public class RunLengthEncoding {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter input string: ");
    String s = input.nextLine();
    for (int a = 0; a < s.length(); a++) {
        if ((s.charAt(a) < 'A' || s.charAt(a) > 'Z')) {
            System.out.print("Bad input");
            System.exit(0);
        }
    }
    System.out.print("Enter flag character: ");
    char flag = input.nextLine().charAt(0);
    if (flag == '#' || flag == '$' || flag == '*' || flag == '&') {

        int count = 0;
        for (int i = 1; i < s.length(); i++) {
            if(s.charAt(i)=s.charAt(i-1));
            count++;

            if (count == 1)
                System.out.print(s.charAt(i));
            if (count == 2)
                System.out.print(s.charAt(i) + s.charAt(i));
            if (count == 3)
                System.out.print(s.charAt(i) + s.charAt(i) + s.charAt(i));
            else
                System.out.print(flag + s.charAt(i) + (count + 1));

        }

    } else
        System.out.print("Bad input");
}
}

&#34; TestRuns&#34;行导致过多的查询。知道我在这里做错了吗?

感谢您的任何见解。

1 个答案:

答案 0 :(得分:1)

测试的答案很难,但我认为我们可以避免分组和多次查询:(https://msdn.microsoft.com/en-us/library/bb311040.aspx

           var nightlyRuns = (from a in LoadTestSummaries
            join b in LoadTestTestSummaryData
            on a.LoadTestRunId equals b.LoadTestRunId
            where a.TargetStack == "LoadEnv" &&
            a.TestGuid != null &&
            a.StartTime != null &&
            a.LoadTestRunId != null
            into testGroup

            select new 
            {
                TestGuid = a.TestGuid,
                ScenarioRun = new 
                {
                    Name = a.TestGuid,
                    Description = a.Description,
                    StartTime = a.StartTime ?? a.DateCreated,
                    Duration = a.Duration,
                    NumAgents = g.Key.NumAgents,
                    NumHosts = a.NumHosts,
                    Result = a.PassFail,
                    ResultsFilePath = a.ResultsFilePath,
                    SplunkLink = a.Splunk,
                    // PROBLEM: Causes too many queries:
                    TestRuns =testGroup
                }
            }).OrderBy(x=>x.StartTime).ToLookup(x => x.TestGuid, x => x.ScenarioRun);

            nightlyRuns["ba593f66-695f-4fd1-99c3-71253a2e4981"].Dump();