2016-09-22 64 views
0

我有一個SQL查詢,我試圖轉換爲LINQ,並且無法在枚舉查詢時理解錯誤的錯誤消息。轉換SQL與LINQ聯接時出錯

SQL查詢(用作意),是:

select a.TestGuid, MIN(a.StartTime) as StartTime, COUNT(b.TestCaseId) as NumTests, COUNT(DINSTINCT a.Id) as NumScenarios 
from LoadTestSummary as a 
join LoadTestTestSummaryData as b 
    on a.LoadTestRunid = b.LoadTestRunId 
where 
    a.TargetStack = env and 
    a.TestGuid IS NOT NULL AND 
    a.StartTime IS NOT NULL AND 
    a.LoadTestRunId IS NOT NULL 
group by a.TestGuid 

轉換爲LINQ,我得到如下:

var q = from a in _context.LoadTestSummary 
     where 
      a.TargetStack == env && 
      a.TestGuid != null && 
      a.StartTime != null && 
      a.LoadTestRunId != null 
     join b in _context.LoadTestTestSummaryData on new 
     { 
      LoadTestRunId = Convert.ToInt32(a.LoadTestRunId) 
     } equals new 
     { 
      LoadTestRunId = b.LoadTestRunId 
     } 
     group new { a, b } by new 
     { 
      a.TestGuid 
     } 
     into g 
     select new 
     { 
      DateCreated = g.Min(p => p.a.StartTime), 
      NumScenarios = g.Count(), 
      TestGuid = g.Key.TestGuid 
      NumTests = // ??? 
     }; 

兩個問題,我有:

1)當查詢枚舉時,我得到一個運行時錯誤,我無法解密。該查詢在Linqpad中工作正常,但在我的程序中給我一個運行時錯誤。我不確定這會導致什麼。而就在這盯着讓我的腦袋疼:

ArgumentException: Expression of type 'System.Func``2[Microsoft.Data.Entity.Query.EntityQueryModelVisitor+TransparentIdentifier``2[PerfPortal.Models.LoadTestSummary,PerfPortal.Models.LoadTestTestSummaryData],<>f__AnonymousType7``1[System.String]]' cannot be used for parameter of type 'System.Func``2[<>f__AnonymousType5``2[PerfPortal.Models.LoadTestSummary,PerfPortal.Models.LoadTestTestSummaryData],<>f__AnonymousType7``1[System.String]]' of method 'System.Collections.Generic.IEnumerable``1[System.Linq.IGrouping``2[<>f__AnonymousType7``1[System.String],<>f__AnonymousType5``2[PerfPortal.Models.LoadTestSummary,PerfPortal.Models.LoadTestTestSummaryData]]] _GroupBy[<>f__AnonymousType5``2,<>f__AnonymousType7``1,<>f__AnonymousType5``2](System.Collections.Generic.IEnumerable``1[<>f__AnonymousType5``2[PerfPortal.Models.LoadTestSummary,PerfPortal.Models.LoadTestTestSummaryData]], System.Func``2[<>f__AnonymousType5``2[PerfPortal.Models.LoadTestSummary,PerfPortal.Models.LoadTestTestSummaryData],<>f__AnonymousType7``1[System.String]], System.Func``2[<>f__AnonymousType5``2[PerfPortal.Models.LoadTestSummary,PerfPortal.Models.LoadTestTestSummaryData],<>f__AnonymousType5``2[PerfPortal.Models.LoadTestSummary,PerfPortal.Models.LoadTestTestSummaryData]])'

2)我不太清楚如何讓COUNT(DISTINCT a.Id)到NumTests領域。看起來這不是LINQ支持的,但它看起來像其他人已經問過這個問題,所以我可能能夠弄清楚#1解決了。

這裏有什麼不對嗎?我甚至不確定錯誤告訴了我什麼。

所有幫助表示讚賞!

回答

1

在SQL查詢和您的LINQ代碼展望只是,我想出了這樣的事情:

from a in LoadTestSummary 
join b in LoadTestTestSummaryData 
    on a.LoadTestRunId equals b.LoadTestRunId 
where 
    a.TargetStack == env && 
    a.TestGuid != null && 
    a.StartTime != null && 
    a.LoadTestRunId != null 
group new { a, b } by a.TestGuid into g 
select new 
{ 
    TestGuid = g.Key, 
    DateCreated = g.Min(el => el.a.StartTime), 
    NumTests = g.Select(el => el.b.TestCaseId).Count(), 
    NumScenarios = g.Select(el => el.a.Id).Distinct().Count() 
}; 

注意,你不需要LoadTestRunId轉換爲int,你可能只需要使用標準的字符串比較。

這個可怕的錯誤很可能是由於使用無名對象進行分組和比較而引起的,所以我寧願不要太多地讀這個錯誤,因爲它似乎是一種不易被人看見或理解的可惡憎惡。

+0

謝謝Gasper,但它似乎給出了同樣的錯誤。我知道它與連接有關,因爲當我刪除它(以及NumTests字段)時,查詢工作正常。我應該澄清你的查詢在Linqpad中工作正常......所以也許我的生成模型有問題。我會嘗試重建。 –

+0

這很奇怪。你也可以嘗試更新這個問題,看看你的表的類是怎麼看的。 – Gasper

+0

同意。我將嘗試從數據庫重建模型並更新問題。 –