LINQ to Entities无法识别该方法(分区/行号)
本文关键字:分区 行号 方法 Entities to 识别 LINQ | 更新日期: 2023-09-27 18:21:44
我一辈子都搞不清楚为什么这个查询没有运行
它编译正确,但在运行时尝试执行.ToList()时失败,代码后面显示错误。
我已经复习了类似的答案,但似乎每个答案都有各自的问题。
我把Query分为多行,试图缩小有问题的代码,它似乎在"//Partition Over and Produce RowCount for each Partition"之后的一行,但我搞不清楚。
我借用了GroupBy/Partition的逻辑:Linq中的Row_number over(Partition by xxx)?
[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无法识别方法"System.Collections.Generic.IEnumerable"1[WebRole1.Tests.Checksum]Zip[Signin,Int32,Checksum](System.Collections.Generic.IEnumberable"1[iSignRepo.Models.Signin],System.Collections.General.IEnumerable'1[System.Int32],System.Func`3[iSignRepo.Models.Signin,System.Int32,WebRole1.Tests.Checksum])"方法,并且该方法不能被翻译成存储表达式。
基本上,它无法将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 }));