簡體   English   中英

無法從 Linq 查詢 EF Core 訪問字段值

[英]Cannot access the field value from the Linq query EF Core

我正在嘗試構建一個 .NET 核心應用程序,該應用程序使用 EF 核心並使用緩存來存儲一些查詢響應。 我在訪問 LINQ 查詢響應中的字段時遇到問題。 我不確定我在這里缺少什么。 SQL Server DB中有一個Setting表,模型如下

public partial class Setting
{
    public string EndTime { get; set; }
    public string StartTime { get; set; }
    public string OrderDay { get; set; }
    public int SettingsId { get; set; }
}

並嘗試在名為 CustomersController 的控制器中像下面一樣查詢它

  public class CustomersController : Controller
  {
    ............
    IQueryable<JAXSurplusMouseApp.Models.Setting> settings_data = null;
    .....................
    bool isSettingExist = memoryCache.TryGetValue("SMSettings", out settings_data);
    if (!isSettingExist)
        {
            string myCondition = (todayDt.DayOfWeek.ToString().Substring(0, 3)).ToUpper();
            settings_data = (from s in _context.Settings
                            where s.OrderDay == myCondition
                            select s).Take(1);
            var cacheEntryOptions = new MemoryCacheEntryOptions()
                .SetSlidingExpiration(TimeSpan.FromHours(2));

            memoryCache.Set("SMSettings", settings_data, cacheEntryOptions);
        }

        if(settings_data.Count() != 0)
        {
            stringstart_time = settings_data.StartTime;
        }

我收到諸如'IQueryable<Setting>' does not contain a definition for 'StartTime' and no accessible extension method 'StartTime' accepting a first argument of type 'IQueryable<Setting>' could be found (are you missing a using directive or an assembly reference?)'IQueryable<Setting>' does not contain a definition for 'StartTime' and no accessible extension method 'StartTime' accepting a first argument of type 'IQueryable<Setting>' could be found (are you missing a using directive or an assembly reference?)錯誤'IQueryable<Setting>' does not contain a definition for 'StartTime' and no accessible extension method 'StartTime' accepting a first argument of type 'IQueryable<Setting>' could be found (are you missing a using directive or an assembly reference?)我確定我在這里遺漏了一些東西,但不確定我遺漏了什么任何幫助都非常感謝

:)
問題主要是它仍然是 IQerable<> 並且您需要實際實例:

    stringstart_time = settings_data.First().StartTime;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM