1

呃在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();
4

1 回答 1

1

你不能用字典来做到这一点。另一种方法是将三个 linq 查询合并为一个。您可以通过不使用ToArray. 这将离开com并允许您使用它们编写其他查询。codeIQueryable<T>

您还需要使用 agroup而不是构造字典。像这样的东西应该工作:

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();

旁注:我还建议尽可能使用导航属性而不是连接。它使阅读和理解对象之间的关系以及创建复杂查询变得更加容易。

于 2013-04-09T15:41:52.050 回答