[英]LINQ to Entities does not recognize the method 'System.String StringConvert(System.Nullable`1[System.Double])
[英]LINQ to Entities does not recognize the method '<>f__AnonymousType9`2[System.Nullable`1[System.Int32],System.Nullable`
我在C#中執行linq表達式后出錯。 我可以在調試時看到where子句中的值,但不確定為什么我會得到null異常
以下是錯誤
'LINQ to Entities does not recognize the method '<>f__AnonymousType9`2[System.Nullable`1[System.Int32],System.Nullable`1[System.Int32]] get_Item(Int32)' method, and this method cannot be translated into a store expression.'
我得到ID的第一步
var benchmarks = GetService<MANAGERSTRATEGY>().Where(x => x.ID == id).Select(x => new { x.BENCHMARK1_ID, x.BENCHMARK2_ID }).ToList();
第二步是根據值獲取ID
var indexPerformance = GetViewService<MV_INDEX_PERFORMANCE>().Where(x=> x.IndexId == benchmarks[0].BENCHMARK1_ID || x.IndexId == benchmarks[0].BENCHMARK2_ID);
該模型用於oracle中的物化視圖
public class MV_INDEX_PERFORMANCE : ViewEntity
{
[Column("INDEX_LIST_FIELD_ID")] public int IndexListFieldId { get; set; }
[Column("TICKER")] public string Ticker { get; set; }
[Column("INDEX_ID")] public int IndexId { get; set; }
[Column("CCY_ID")] public int? CcyId { get; set; }
[Column("CCY_CODE")] public string CcyCode { get; set; }
[Column("FIELD_ID")] public int? FieldId { get; set; }
[Column("FIELD_CODE")] public string FieldCode { get; set; }
[Column("FIELD_NAME")] public string FieldName { get; set; }
[Column("INDEX_NAME")] public string IndexName { get; set; }
[Column("PRICE_DATE")] public DateTime PriceDate { get; set; }
[Column("PRICE_DATE_ACTUAL")] public DateTime PriceDateActual { get; set; }
[Column("PRICE")] public int? Price { get; set; }
[Column("MTD")] public int? Mtd { get; set; }
[Column("MTD_MINUS_THREE")] public int? MtdMinusThree { get; set; }
[Column("QTD")] public int? Qtd { get; set; }
[Column("YTD")] public int? Ytd { get; set; }
[Column("SI")] public int? Si { get; set; }
}
GetViewService
public IViewService<X> GetViewService<X>() where X : ViewEntity, new()
{
return IoC.Resolve<IViewService<X>>();
}
IViewService中的where方法
public IEnumerable<T> Where(Expression<Func<T, bool>> predicate)
{
try
{
using (new TimedLogger(_perfLogger, GetCompletedText("Where")))
{
return Authorize(_repo.Where(predicate).ToList(), AuthAccessLevel.Read);
}
}
catch (Exception ex)
{
_logger.Error(ex);
throw;
}
}
即使這些值看起來像檢查工具提示中的int,要顯示它們VS正在評估/遍歷您的匿名類型以獲取要顯示給您的實際值。
編譯器需要在編譯時構造一個執行相同操作的表達式,並且它將在表達式中包含類似的邏輯,以便可以在運行時執行它以獲取值。
然后將相同的謂詞/表達式傳遞到您的存儲庫,其中EF嘗試從表達式生成SQL,並且缺乏能夠這樣做的復雜性(數據庫模型將無法識別您的匿名類型),因此該部分在關於無法“創建商店表達式”的錯誤中(在這種情況下為SQL)。 這只是映射器的限制; 它可以很好地管理簡單類型,但會很難解決這個問題。
如果將2個值復制到局部變量中並使用謂詞中的值,則應該沒問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.