Use method in entity framework query

时光总嘲笑我的痴心妄想 提交于 2019-12-13 04:41:24

问题


Is there anyway around this error? I'd like to reuse the same lamba expression in other queries instead of having duplication. Can LinqKit or other linq expression do this?

Error

LINQ to Entities does not recognize the method 'Boolean GetEvent(Tournaments.Data.Entities.Event, System.String)' method, and this method cannot be translated into a store expression.

Code

public MobileEventDetailModel GetDetails(string applicationId)
{
    var @event = (from e in _eventsRepository.DataContext.Events.Include(q => q.Assets.Select(a => a.Asset))
                  where GetEvent(e, applicationId)
                select new
                    {
                        e.Id,
                        e.EventParent.Name,
                        LogoId = (from a in e.Assets
                                     where a.Type   == EventAssetType.Logo
                                     select a.AssetId).FirstOrDefault()
                    }).FirstOrDefault();

    return new MobileEventDetailModel
        {
            Id = @event.Id,
            Name = @event.Name,
            Logo = string.Format("{0}{1}{2}", Config.BaseUrl, Config.ImagesPath, @event.LogoId)
        };
}

public bool GetEvent(Event @event, string applicationId)
{

    return @event.Active && @event.Visible && @event.MobileEventApplications.Any(m =>
                          m.MobileApplication.ApplicationId == applicationId &&
                          (!m.MobileApplication.ActivationLength.HasValue || EntityFunctions.AddDays(DateTime.Now, 1) < EntityFunctions.AddMonths(m.MobileApplication.DateActivated, m.MobileApplication.ActivationLength.Value)));
}

回答1:


You need to use an Expression:

public MobileEventDetailModel GetDetails(string applicationId)
{

    var event = _eventsRepository.DataContext.Events.Include(q => q.Assets.Select(a => a.Asset))
                .Where(GetEvent(applicationId))
                .Select(a =>  new
                    {
                        a.Id,
                        a.EventParent.Name,
                        LogoId = (from b in a.Assets
                                     where b.Type == EventAssetType.Logo
                                     select b.AssetId).FirstOrDefault()
                    }).FirstOrDefault();

    return new MobileEventDetailModel
        {
            Id = event.Id,
            Name = event.Name,
            Logo = string.Format("{0}{1}{2}", Config.BaseUrl, Config.ImagesPath, event.LogoId)
        };
}

public Expression<Func<Event, bool>> GetEvent(int applicationId)
{
    return = a => a.Active 
                  && a.Visible 
                  && a.MobileEventApplications
                      .Any(m => m.MobileApplication.ApplicationId == applicationId 
                                && (!m.MobileApplication.ActivationLength.HasValue 
                                    || EntityFunctions.AddDays(DateTime.Now, 1) 
                                       < EntityFunctions
                                         .AddMonths(m.MobileApplication.DateActivated, m.MobileApplication.ActivationLength.Value)
                                   )
                      );
}

Update: Sorry it was late the other night, the changed version is hopefully more what you were looking for.



来源:https://stackoverflow.com/questions/19761632/use-method-in-entity-framework-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!