Join two tables and filter result using EF 4 and RIA services

不羁的心 提交于 2019-12-12 03:18:06

问题


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... :(


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/9079930/join-two-tables-and-filter-result-using-ef-4-and-ria-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!