LINQ在where子句中使用字典

时间:2013-04-09 15:21:16

标签: c# linq linq-to-sql dictionary linq-to-entities

在linq中有以下查询...

每当我尝试运行它时,我都会遇到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 个答案:

答案 0 :(得分:1)

您将无法使用字典执行此操作。另一种方法是将三个linq查询合并为一个。如果不使用ToArray实现查询,则可以在对代码影响最小的情况下执行此操作。这会将comcode保留为IQueryable<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而不是连接。它使阅读和理解对象如何相关并创建复杂查询变得更加容易。