只能从Linq到Entity Framework

时间:2017-09-29 04:26:59

标签: c# entity-framework linq

为什么底层linq到实体工作但是被评论的最高查询不起作用?

var yogaProfile = dbContext.YogaProfiles.Where(i => i.ApplicationUserId == userId).First();
var yogaSpace = yogaProfile.YogaSpaces.Where(j => j.YogaSpaceId == yogaSpaceId).First();

if (yogaSpace == null)
 throw new Exception("You Don't Have A Yoga Space With This ID!");

var todayPlus30 = DateTime.Now.AddDays(30);
var yesterday = DateTime.Now.Date.AddDays(-1);

// Does not work!!
//var yogaEvents = yogaSpace.YogaSpaceEvents.Where(k => 
//    DbFunctions.TruncateTime(k.EventDateTime) > DbFunctions.TruncateTime(yesterday) && 
//    DbFunctions.TruncateTime(k.EventDateTime) < DbFunctions.TruncateTime(todayPlus30)).ToList();


var yogaEvents = yogaSpace.YogaSpaceEvents.Where(k =>
 k.EventDateTime.Date > yesterday &&
 k.EventDateTime.Date < todayPlus30);

1 个答案:

答案 0 :(得分:1)

问题是您没有在正确的上下文中使用this.props.navigation.navigate('Login') 。这些可以在Linq-To-Entities中使用,但是您已经使用实体框架获取了DbFunctions个对象。

现在,您正尝试从另一个IEnumerable类型数据中获取数据 yogaSpace因此它不适用于YogaSpaceEvents上下文。

在这种情况下,您可以使用标准日期操作来解决问题..

参见参考文献:
Linq Exception: Function can only be invoked from linq to entities
linq exception : This function can only be invoked from LINQ to Entities
EF extension method: "This function can only be invoked from LINQ to Entities."