2013-04-04 62 views
0

我有一個WCF查詢數據實體的數據。 我有點困惑,爲什麼它抱怨「方法不支持」,如果語法沒有把「AsEnumerable」,它有一種不使用AsEnumerable的方式。因爲我讀過一些提到的文章,它會在執行「where」條件之前放入所有數據。LINQ「方法加入不支持」當把「AsEnumerable」去掉

 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 

回答

0

AsEnumerableJoin語句執行到LINQ到對象。因此,所有來自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} 

而且可能的優化 - 不知道Contains()在WCF地調用,所以你必須自己去檢查一下。

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} 
+0

這會減少行數返回嗎?對於上面的代碼,RC_CampaignHubSpokeTbl仍然會返回所有行,並且RC_CampaignTbl將僅返回tintstatus = 1? – tsohtan 2013-04-04 09:47:10

+0

是的,但僅限於一張桌子。它將加載所有HubSpokes,但僅加載必要的Campaps。 – MarcinJuraszek 2013-04-04 09:49:55

+0

好的。謝謝。嗯......我認爲如果數據增長,長期來看,它會有性能問題。 – tsohtan 2013-04-04 09:52:30