使用 EF 4 和 RIA 服务连接两个表并筛选结果

发布于 2025-01-01 17:55:32 字数 536 浏览 5 评论 0原文

Public Function LoadSiteInfo(ByVal sId As Integer) As IQueryable(Of Site)
    Return Me.ObjectContext.Sites.Include("SiteData").Where((Function(f) f.SiteID = sId) AndAlso Function(x) x.SiteData.SiteUpdateDate < today)) 
End Function

因此,我试图从 Sites 表中过滤 SiteId,并在 SiteData 表中的 SiteUpdateDate 上进行过滤,这是我无法获得正确语法的最后一部分 - 它只是说 SiteUpdateDate 属性不是 ObjectContext.Site 的成员,这是正确的 SiteData 部分

如何过滤包含的表 SiteData 中的属性?或者我如何重写它并仍然返回一个可查询的站点,其中 SiteData 子表已根据我的需要进行过滤?应该很容易,但我很挣扎,开始相信不允许包含选定父级的过滤子集合......:(

Public Function LoadSiteInfo(ByVal sId As Integer) As IQueryable(Of Site)
    Return Me.ObjectContext.Sites.Include("SiteData").Where((Function(f) f.SiteID = sId) AndAlso Function(x) x.SiteData.SiteUpdateDate < today)) 
End Function

So, I'm trying to filter on SiteId from the Sites table, ANDALSO on the SiteUpdateDate in the SiteData table, it's the last part where I cannot get the syntax correct - it's just saying that SiteUpdateDate attribute is not a member of ObjectContext.Site, which is correct its part of SiteData

How can I filter on attributes in the included table SiteData? Or how can I rewrite this and still return an Iquerable of Site with SiteData childtable beeing filtered as I want? Should be really easy, but I'm struggling, starting to believe that include a filtered child collection of a selected parent is not allowed... :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仙女山的月亮 2025-01-08 17:55:32

我不相信你可以这样做。您必须首先将子集合返回给您的父集合。

Public Function LoadSiteInfo(ByVal sId As Integer) As IQueryable(Of Site)
     return FillSiteInfo(sId).Where(Function(x) x.SiteData.SiteUpdateDate < today))
End Function 

Public Function FillSiteInfo(byVal sId as Integer) as IQueryable(of Site)
    Return Me.ObjectContext.Sites.Include("SiteData").Where((Function(f) f.SiteID = sId).AsQueryable()
End Function

那应该对你有用。

您还需要检查您的 RIA 服务,以确保您的子集合具有 [include] 作为属性。

I dont believe you can do it this way. You have to first return your parent with child collections.

Public Function LoadSiteInfo(ByVal sId As Integer) As IQueryable(Of Site)
     return FillSiteInfo(sId).Where(Function(x) x.SiteData.SiteUpdateDate < today))
End Function 

Public Function FillSiteInfo(byVal sId as Integer) as IQueryable(of Site)
    Return Me.ObjectContext.Sites.Include("SiteData").Where((Function(f) f.SiteID = sId).AsQueryable()
End Function

That Should work for you.

also you need to check your RIA services to make sure your Child Collection has [include] as an attribute.

自由如风 2025-01-08 17:55:32

我不太擅长 VB,所以这里是 C# 版本。

return this.ObjectContext
   .Sites
   .Where(x => x.SiteData.All(y => y.SiteUpdateData < today))
   .Where(x => x.SiteId == sId)
   .Select(x => new { Sites = x, SiteDatas = x.SiteData })
   .Select(x => x.Sites);

技巧是,您无法过滤急切加载的属性 (SiteData),因此您必须使用匿名类型投影,然后将查询投影回您想要的内容。

I'm not good with VB, so here's the C# version.

return this.ObjectContext
   .Sites
   .Where(x => x.SiteData.All(y => y.SiteUpdateData < today))
   .Where(x => x.SiteId == sId)
   .Select(x => new { Sites = x, SiteDatas = x.SiteData })
   .Select(x => x.Sites);

The trick is, you can't filter on an eager loaded property (SiteData), so you have to use anonymous type projection, and then project the query back into what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文