2013-04-09 61 views
1

ER在LINQ以下查詢......LINQ使用的字典在where子句

每當我嘗試運行它,我得到一個No comparison operator for type System.Int[]例外。

這是有關與字典,我敢肯定,但我不明白爲什麼這是無效的,並想知道如果有人可以解釋?

// As requested... not sure it will help though. 
var per = (
    from p in OtherContext.tblPeriod 
    where activeContractList.Select(c => c.DomainSetExtensionCode).Contains(p.DomainSetExtensionCode) 
    select p).ToArray(); 

var com = (
    from c in MyContext.tblService 
    join sce in MyContext.tblServiceExtension 
    on c.ServiceExtensionCode equals sce.ServiceExtensionCode 
    join sc in MyContext.tblServiceContract 
    on sce.ServiceContractCode equals sc.ContractCode 
    group sc by c.Period into comG 
    select new 
    { 
     PeriodNumber = comG.Key, 
     Group = comG, 
    }).ToArray(); 

var code = 
    (from c in com 
    join p in per on c.PeriodNumber equals p.PeriodNumber 
    select new 
    { 
     p.Code, 
     c.Group 
    }).ToArray(); 

var payDictionary = new Dictionary<int, int[]>(); 

// This is another linq query that returns an anonymous type with 
// two properties, and int and an array. 
code.ForEach(c => payDictionary.Add(c.Code, c.Group.Select(g => g.Code).ToArray())); 

// MyContext is a LINQ to SQL DataContext 
var stuff = (
from 
    p in MyContext.tblPaySomething 
    join cae in MyContext.tblSomethingElse 
    on p.PaymentCode equals cae.PaymentCode 
    join ca in MyContext.tblAnotherThing 
    on cae.SomeCode equals ca.SomeCode 
where 
    // ca.ContractCode.Value in an int?, that should always have a value. 
    payDictionary[p.Code].Contains(ca.ContractCode.Value) 
select new 
{ 
    p.Code, 
    p.ExtensionCode, 
    p.IsFlagged, 
    p.Narrative, 
    p.PayCode, 
    ca.BookCode, 
    cae.Status 
}).ToList(); 
+1

'ca.ContractCode.Value'是一個'int []'? – 2013-04-09 15:23:44

+2

什麼是'code'? – 2013-04-09 15:23:47

+0

你可以包含'MyContext'的定義嗎? – 2013-04-09 15:25:30

回答

1

您將無法使用字典來做到這一點。另一種方法是將三個linq查詢合併爲一個。您可以通過使用ToArray未實現查詢來以最小的影響對代碼執行此操作。這將使comcodeIQueryable<T>,並允許您編寫其他查詢。

您還需要使用group而不是構建字典。像這樣的東西應該工作:

var per = (
    from p in OtherContext.tblPeriod 
    where activeContractList.Select(c => c.DomainSetExtensionCode).Contains(p.DomainSetExtensionCode) 
    select p.PeriodNumber).ToArray(); // Leave this ToArray because it's materialized from OtherContext 

var com = 
    from c in MyContext.tblService 
    join sce in MyContext.tblServiceExtension on c.ServiceExtensionCode equals sce.ServiceExtensionCode 
    join sc in MyContext.tblServiceContract on sce.ServiceContractCode equals sc.ContractCode 
    group sc by c.Period into comG 
    select new 
    { 
     PeriodNumber = comG.Key, 
     Group = comG, 
    }; // no ToArray 

var code = 
    from c in com 
    where per.Contains(c.PeriodNumber) // have to change this line because per comes from OtherContext 
    select new 
    { 
     Code = c.PeriodNumber, 
     c.Group 
    }; // no ToArray 

var results = 
    (from p in MyContext.tblPaySomething 
    join cae in MyContext.tblSomethingElse on p.PaymentCode equals cae.PaymentCode 
    join ca in MyContext.tblAnothThing on cae.SomeCode equals ca.SomeCode 
    join cg in MyContext.Codes.GroupBy(c => c.Code, c => c.Code) on cg.Key equals p.Code 
    where cg.Contains(ca.ContractCode.Value) 
    select new 
    { 
     p.ContractPeriodCode, 
     p.DomainSetExtensionCode, 
     p.IsFlagged, 
     p.Narrative, 
     p.PaymentCode, 
     ca.BookingCode, 
     cae.Status 
    }) 
    .ToList(); 

側面說明:我使用navigation properties如果可能的話,而不是加入也建議。它使得閱讀和理解對象如何相關以及創建複雜查詢變得更加容易。

+0

通常L2S或EF不會接受使用多個上下文的單個查詢。所以我希望這不起作用。 – Maarten 2013-04-09 15:54:01

+0

@Maarten我沒有使用多個上下文。注意'per'仍然物化爲一個數組。我在答覆中加入了一個註釋,以便更清楚地說明問題。 – 2013-04-09 15:55:46

+0

是的,我的錯。儘管如此,您正在將一個查詢的結果用作另一個查詢的輸入,其中數據不是值類型列表(本例中爲tblPeriod中的記錄數組)。我仍然認爲這是行不通的。一個查詢的輸入必須是一個值類型,或者它的一個集合(可枚舉)。 – Maarten 2013-04-09 16:04:20