【发布时间】:2013-11-04 04:24:36
【问题描述】:
这个错误有什么问题吗?我想在其他查询中重用相同的 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))
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)));
}
【问题讨论】:
-
我不认为 LINQkit 或任何其他扩展可以提供帮助。您不能使用 .net 中的方法或函数,因为它们无法转换为 SQL。您可以使用包含标准 Sql Server 函数的 SqlFunctions。
标签: linq entity-framework lambda entity-framework-6