Home > Mobile >  Cannot access the field value from the Linq query EF Core
Cannot access the field value from the Linq query EF Core

Time:11-15

I am trying to build a .NET core application that uses EF core and alos using Caching to store some of the query response. I am having issue with accessing fields from the LINQ query response. I am not sure what is that I am missing here. There is a Setting table in the SQL Server DB and the model is like below

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

and trying to query it like below in a Controller called 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;
        }

I get error like '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?) I am sure I am missing something here but not sure what is that I am missing any help is greatly appreciated

CodePudding user response:

:)
Issue mainly is that it's an IQuerable<> still and you need the actual instance:

    stringstart_time = settings_data.First().StartTime;
  • Related