连接两个表并使用EF 4和RIA服务过滤结果 [英] Join two tables and filter result using EF 4 and RIA services

查看:75
本文介绍了连接两个表并使用EF 4和RIA服务过滤结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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上的ANDALSO,这是我无法正确获取语法的最后一部分-只是说SiteUpdateDate属性不是ObjectContext.Site的成员,那是对的,它是SiteData的一部分。

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

如何过滤包含的表SiteData中的属性?或者,我该如何重写它,并且仍然返回一个具有SiteData子级表蜜蜂的Iquerable网站?应该真的很简单,但是我很挣扎,开始相信不允许包含选定父级的过滤子级集合...:(

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

推荐答案

我对VB不好,所以这里是C#版本。

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);

诀窍是,您无法筛选渴望的加载属性( SiteData ),因此您必须使用匿名类型投影,然后将查询投影回您想要的内容。

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.

这篇关于连接两个表并使用EF 4和RIA服务过滤结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆