当我执行下面的代码时,我得到了这个错误,有什么想法可以修复它吗?
LINQ to Entities does not recognize the method 'Int32 Min(Int32, Int32)' method, and this method cannot be translated into a store expression.result = items.ToList()
.Select(b => new BatchToWorkOnModel()
{
BatchID = b.Batch.ID,
SummaryNotes = b.Batch.Notes,
RowVersion = b.Batch.RowVersion,
Items = items
.Select(i => new ItemToWorkOnModel()
{
SupplierTitle = i.Title,
ItemID = i.ID,
BatchID = i.BatchID ?? 0,
ItemDate = i.PubDate,
// KB - Issue 276 - Return the correct Outlet name for each item
Outlet = i.Items_SupplierFields != null ? i.Items_SupplierFields.SupplierMediaChannel != null ? i.Items_SupplierFields.SupplierMediaChannel.Name : null : null,
Status = ((short)ItemStatus.Complete == i.StatusID ? "Done" : "Not done"),
NumberInBatch = i.NumInBatch,
Text = string.IsNullOrEmpty(i.Body) ? "" : i.Body.Substring(0, Math.Min(i.Body.Length, 50)) + (i.Body.Length < 50 ? "" : "..."),
IsRelevant = i.IsRelevant == 1,
PreviouslyCompleted = i.PreviouslyCompleted > 0 ? true : false
}).ToList()
})
.FirstOrDefault();发布于 2013-07-22 08:48:32
Math.Min似乎不是由EF查询提供程序实现的。您应该能够通过简单地将AsEnumerable应用于您的items集合来使用Linq对对象执行表达式来修复它;
Items = items.AsEnumerable().Select(i => new ItemToWorkOnModel()...如果将where条件添加到项选择中(在整个表中获取所有项似乎有点奇怪),则需要在AsEnumerable()之前添加它,以允许EF在数据库中进行筛选。
另外,您只需要查询的第一个结果,但是在将列表削减到单个项之前,您将使用ToList()获取所有结果。您可能希望删除ToList(),以便EF/基础数据库只能返回一个结果;
result = items.Select(b => new BatchToWorkOnModel()...发布于 2013-07-22 12:51:34
您不需要Math.Min。
所讨论的问题是:
Text = string.IsNullOrEmpty(i.Body)
? "" : i.Body.Substring(0, Math.Min(i.Body.Length, 50)) + (i.Body.Length < 50 ? "" : "...")那么这条线会返回什么呢?
如果i.Body为空或空,则返回空字符串。如果它是50个或更长的字符,则返回50个字符的子字符串并附加“.”。
如果长度小于50,则接受一个带有字符串长度的子字符串,并附加一个空字符串。但那只是原来的绳子。
Text = string.IsNullOrEmpty(i.Body)
? "" : (i.Body.Length < 50 ? i.Body : i.Body.Substring(0, 50) + "...")https://stackoverflow.com/questions/17783094
复制相似问题