LINQ to Entities无法识别方法(Partition / Rownumber)

时间:2015-11-12 14:44:20

标签: c# entity-framework linq

我不能为我的生活弄清楚为什么这个查询没有运行

它正确编译,但在运行时尝试使用下面显示的错误执行.ToList()时失败。

我已经审核了类似的答案,但似乎每个答案都与他们的问题有关。

我将查询分成多行以尝试缩小有问题的代码,并且它似乎在" //为每个分区分配和生成RowCount之后就行了#34;但我无法弄清楚。

我从这里借用了GroupBy / Partition的逻辑: Row_number over (Partition by xxx) in Linq?

[DataContract(Name = "Checksum")]
public class Checksum
{
    [DataMember(Name = "SortColumn")]
    public DateTime SortColumn { get; set; }
    [DataMember(Name = "Identifier")]
    public string Identifier { get; set; }
    [DataMember(Name = "Seqnum")]
    public int Seqnum { get; set; }
}

public void TestLinQ()
{
    myObjectContext context = new myObjectContext();

    DateTime startDate = new DateTime();

    IQueryable<Signin> iQ = context.Signin;
      iQ = iQ.OrderBy(o => o.LastUpdateTimeStamp);
      iQ = iQ.Where(x=>x.LastUpdateTimeStamp == startDate);

    // Partition Over and Produce RowCount for each partition
    IQueryable<Checksum> iQ2 = iQ.GroupBy(x => x.LastUpdateTimeStamp).Select(g => new { g, count = g.Count() }).SelectMany(t => t.g.Select(b => b).Zip(Enumerable.Range(1, t.count), (j, i) => new Checksum { SortColumn = j.LastUpdateTimeStamp, Identifier = j.SigninId, Seqnum = i }));

    iQ2 = iQ2.Where(x => x.Seqnum < 1000);

    // Build Checksum Code

    List<Checksum> outlist = iQ2.ToList();

    // End Build Checksum Code
}

结果讯息:

  

测试方法WebRole1.Tests.ContinuationTokenTests.TestLinQ抛出异常:   System.NotSupportedException:LINQ to Entities无法识别方法&#39; System.Collections.Generic.IEnumerable&#39; 1 [WebRole1.Tests.Checksum] Zip [Signin,Int32,Checksum](System.Collections.Generic.IEnumerable& #39; 1 [iSignRepo.Models.Signin],System.Collections.Generic.IEnumerable&#39; 1 [System.Int32],System.Func`3 [iSignRepo.Models.Signin,System.Int32,WebRole1.Tests.Checksum ])&#39;方法,并且此方法无法转换为商店表达式。

1 个答案:

答案 0 :(得分:1)

基本上,它无法将Zip转换为SQL。

您需要做的就是在使用该方法之前执行查询

IQueryable<Checksum> iQ2 = iQ.GroupBy(x => x.LastUpdateTimeStamp)
                             .Select(g => new { g, count = g.Count() })
                             .ToList() // Executes the Query
                             .SelectMany(t => t.g.Select(b => b).Zip(Enumerable.Range(1, t.count), (j, i) => new Checksum { SortColumn = j.LastUpdateTimeStamp, Identifier = j.SigninId, Seqnum = i }));