1

次のコードを実行するとこのエラーが発生します。修正方法はありますか?


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

2 に答える 2

2

Math.Min は EF クエリ プロバイダーによって実装されていないようです。AsEnumerable代わりにLinq to Objectsを使用して式を実行するために、項目コレクションに適用するだけで修正できるはずです。

Items = items.AsEnumerable().Select(i => new ItemToWorkOnModel()...

項目の選択に条件を追加する場合where(テーブル全体のすべての項目を取得するのは少し奇妙に思えます)、それを AsEnumerable() の前に追加して、EF がデータベースでフィルタリングを実行できるようにします。

また、クエリからの最初の結果のみが必要ですが、リストを単一のアイテムに切り詰める前に、を使用してすべてをフェッチしています。EF/基になるデータベースが単一の結果のみを返すように、ToList()を削除することができます。ToList()

result = items.Select(b => new BatchToWorkOnModel()...
于 2013-07-22T08:48:32.227 に答える