在实体框架查询中使用方法 [英] Use method in entity framework query

查看:87
本文介绍了在实体框架查询中使用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有关于这个错误?我想在其他查询中重复使用相同的lamba表达式,而不是重复。 LinqKit或其他linq表达式可以这样做吗?



错误


LINQ to Entities不能识别方法Boolean GetEvent(Tournaments.Data.Entities.Event,System.String)方法,并且此方法无法转换为存储表达式。


代码

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

返回新的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&&b $ b(!m.MobileApplication.ActivationLength.HasValue || EntityFunctions.AddDays(DateTime.Now, 1)< EntityFunctions.AddMonths(m.MobileApplication.DateActivated,m.MobileApplication.ActivationLength.Value));
}


解决方案

您需要使用表达式:$ / $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

$ DataContext.Events.Include(q => q.Assets.Select(a => a.Asset))
.Where(GetEvent(applicationId))
.Select(a => new
{
a.Id,
a.EventParent.Name,
LogoId =(从a.Assets中的b
其中b.Type == EventAssetType.Logo
select b.AssetId).FirstOrDefault()
})。FirstOrDefault();

返回新的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现在,1)
< EntityFunctions
.AddMonths(m.MobileApplication.DateActivated,m.MobileApplication.ActivationLength.Value)

);
}

更新:对不起晚了,更改的版本希望更多你在找什么。


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)));
}

解决方案

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.

这篇关于在实体框架查询中使用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆