放入“AsEnumerable”时,LINQ“不支持方法连接”消失了

时间:2013-04-04 09:25:33

标签: vb.net wcf linq asenumerable

我有一个从数据实体查询数据的WCF。 我有点混淆为什么它抱怨“方法加入不支持”如果语法没有放“AsEnumerable”,并且它有一种方法不使用AsEnumerable。因为我读了一些提到的文章,它会在执行“where”条件之前放入所有数据。

        Dim ent As DataEnties.DPPromoterEntities
        Dim que = Nothing
        Dim sRet As String = ""

        Try
            ent = New DataEnties.DPPromoterEntities(New Uri(AppSettings.Item("Data")))

            que = From CHS In ent.RC_CampaignHubSpokeTbl.AsEnumerable  '<--This line 
                  Join Cam In ent.RC_CampaignTbl.AsEnumerable On Cam.intCampaign Equals CHS.intCampaign'<--This line 
                  Where Cam.tintStatus.Equals(1)
                  Select New With {CHS.intCampaign,
                                   CHS.intCouponRefHub,
                                   CHS.intCouponRefSpoke,
                                   CHS.intHubRef,
                                   CHS.intSpokeRef}
            sRet = New JavaScriptSerializer().Serialize(que)

        Catch ex As Exception
            clsLog4Net.WriteLog(System.Reflection.MethodBase.GetCurrentMethod.Name.ToString, ex.Message, True)
        End Try

1 个答案:

答案 0 :(得分:0)

AsEnumerable强制Join语句执行到LINQ to Objects。因此,来自RC_CampaignHubSpokeTblRC_CampaignTbl的所有数据都将从WCF中获取,然后在您的应用程序中合并在一起。

这是必要的,因为WCF调用不支持连接。

要优化您的查询,您可以执行以下操作:

que = From CHS In ent.RC_CampaignHubSpokeTbl.AsEnumerable()
      Join Cam In ent.RC_CampaignTbl.Where(Function(c) c.tintStatus.Equals(1)).AsEnumerable() On Cam.intCampaign Equals CHS.intCampaign
      Select New With {CHS.intCampaign,
                       CHS.intCouponRefHub,
                       CHS.intCouponRefSpoke,
                       CHS.intHubRef,
                       CHS.intSpokeRef}

进一步可能的优化 - 不确定WCF调用中Contains()是否可用,因此您必须自己检查。

Dim compains = ent.RC_CampaignTbl.Where(Function(c) c.tintStatus.Equals(1)).ToArray()
Dim HubSpokes = ent.RC_CampaignHubSpokeTbl.Where(Function(h) compains.Contains(h.intCampaign)).ToArray()

que = From CHS In compains 
      Join Cam In HubSpokes  On Cam.intCampaign Equals CHS.intCampaign
      Select New With {CHS.intCampaign,
                       CHS.intCouponRefHub,
                       CHS.intCouponRefSpoke,
                       CHS.intHubRef,
                       CHS.intSpokeRef}