在我的linq查询

时间:2015-07-24 03:10:21

标签: c# linq entity-framework

我在下面找到了一个无法找到'DateTimeScheduled'

的linq查询
var yogaSpace = (from u in context.YogaSpaces
            orderby u.Address.LocationPoints.Distance(myLocation)
            where ((u.Address.LocationPoints.Distance(myLocation) <= 8047) && (u.Events.DateTimeScheduled >= classDate)) 
            select u).ToPagedList(page, 10);

DateTimeScheduled是红色的,智能感知在u内部找不到任何东西。事件几乎就像它不存在一样。 Intellisnse没有在Events中看到成员。

这是我的YogaSpace和YogaSpaceEvent对象。如果我删除条款“&amp;&amp;(u.Events.DateTimeScheduled&gt; = classDate)”,我可以编译所有内容,此外,我在表格中为我播种用于测试的对象提供了数据!

public class YogaSpace
{
    public int YogaSpaceId { get; set; }

    [Index(IsUnique = false)]
    [Required]
    [MaxLength(128)]
    public string ApplicationUserRefId { get; set; }

    public virtual YogaSpaceOverview Overview { get; set; }

    public virtual YogaSpaceDetails Details { get; set; }

    public virtual ICollection<YogaSpaceImage> Images { get; set; }

    [Required]
    public ListingComplete ImageCompleted { get; set; }

    public byte[] Thumbnail { get; set; }

    public virtual YogaSpaceListing Listing { get; set; }

    public virtual YogaSpaceAddress Address { get; set; }

    public virtual ICollection<YogaSpaceReview> Reviews { get; set; }

    [Required]
    public DateTime DateCreated { get; set; }

    public virtual ICollection<YogaSpaceEvent> Events { get; set; }

    [Required]
    [Index]
    public YogaSpaceStatus Status { get; set; }

    [Required]
    [Range(0, 4)]
    public int StepsToList { get; set; }

    [ForeignKey("ApplicationUserRefId")]
    public virtual ApplicationUser ApplicationUser { get; set; }
}

public class YogaSpaceEvent
{
    public int YogaSpaceEventId { get; set; }
    //public string Title { get; set; }
    [Index]
    //research more about clustered indexes to see if it's really needed here
    //[Index(IsClustered = true, IsUnique = false)] 
    public DateTime DateTimeScheduled { get; set; }
    public int AppointmentLength { get; set; }
    public int StatusEnum { get; set; }

    [Index]
    public int YogaSpaceRefId { get; set; }

    [ForeignKey("YogaSpaceRefId")]
    public virtual YogaSpace YogaSpace { get; set; }
}

1 个答案:

答案 0 :(得分:1)

属性u.Events是一系列事件。因此,DateTimeScheduled不能有单个值,它有多个值。

您需要先选择DateTimeScheduled >= classDate的事件。像这样:

var yogaSpace = (from u in context.YogaSpaces
        orderby u.Address.LocationPoints.Distance(myLocation)
        where ((u.Address.LocationPoints.Distance(myLocation) <= 8047) 
        && (u.Events.Any(e => e.DateTimeScheduled >= classDate))) 
        select u).ToPagedList(page, 10);

代码u.Events.Any(e => e.DateTimeScheduled >= classDate)的更改部分现在将返回一个布尔值true或false,指示是否在课程日期或之后安排了任何事件。