0

I want to do something like this

public string GetSiteTitleFromChangeHistory(int siteId)
{
    var changeHistories = changeHistoryRepository.GetAll(c => c.SiteRegistryId == siteId);
    var value = changeHistories.firstOrDefault(r=>r.State="Active")
    return value.Title;
}

public IQueryable<PendingReconfirmation> GetSitesForBusinessReconfirmationReminder(IList<StateStatus> stateStatusMappingIds,
    string country, int reminderDay)
{
    
    return from reg in repositorySpositeRegistry.GetAll(x => x.SiteUrlcountryCode != null
            && x.SiteUrlcountryCode.ToLower() == country.ToLower())
            
            select new PendingReconfirmation()
            {
                Id = reg.Id,
                SiteTitle = GetSiteTitleFromChangeHistory(reg.Id!).ToString() ?? reg.SiteTitle,
                
            };
}

Repository.GetAll look like this

public IQueryable<T> GetAll(Expression<Func<T, bool>>? filter = null)
{
    var query = entities.AsQueryable();
    if (filter != null)
        query = query.Where(filter);
    return query;
}

But I am getting error

The client projection contains a reference to a constant expression of '' through the instance method 'GetSiteTitleFromChangeHistory'. This could potentially cause a memory leak; consider making the method static so that it does not capture constant in the instance.

But I cant make the method as static . Any help will be highly appreciated

Alias Varghese
  • 2,104
  • 3
  • 24
  • 52

2 Answers2

0

Make this function GetSiteTitleFromChangeHistory as STATIC.

0

Is there a specific reason you split up GetSiteTitleFromChangeHistory into a separate method?

Otherwise you should be able to do a subquery like so, provided the queryable used for the subquery is based on a dbset in the same efcontext:

public IQueryable<PendingReconfirmation> GetSitesForBusinessReconfirmationReminder(IList<StateStatus> stateStatusMappingIds,
                                                                                   string             country, int reminderDay)
{
    return from reg in repositorySpositeRegistry.GetAll(x => x.SiteUrlcountryCode != null
                                                             && x.SiteUrlcountryCode.ToLower() == country.ToLower())
            
           select new PendingReconfirmation()
           {
               Id        = reg.Id,
               SiteTitle = changeHistoryRepository.GetAll(c => c.SiteRegistryId == reg.Id && c.State == "Active").FirstOrDefault().Title ?? reg.Title
           };
}
cnork
  • 11
  • 3