2012-11-19 20 views
0

我有LINQ查詢,如下字符串轉換爲在LINQ的GUID連接查詢:條件語句

using (RMPortalEntities _RMPortalEntities = new RMPortalEntities()) { 

       var _RSVP_ButtonLocations = _RMPortalEntities 
              .tbl_RSVP_ButtonLocation 
              .Join(_RMPortalEntities.tbl_RSVP_Setting, 
                  _RSVP_ButtonLocation => Guid.Parse(_RSVP_ButtonLocation.ID), 
                  _RSVP_Setting => _RSVP_Setting.RSVP_Button_Location_ID, 
                  (_RSVP_ButtonLocation, _RSVP_Setting) => new { _RSVP_ButtonLocation, _RSVP_Setting }) 
              .Join(_RMPortalEntities.tbl_Event, 
                  _RSVP_ButtonLocation_RSVP_Setting => _RSVP_ButtonLocation_RSVP_Setting._RSVP_Setting.EventID, 
                  _Event => _Event.ID, 
                  (_RSVP_ButtonLocation_RSVP_Setting, _Event) => new { _RSVP_ButtonLocation_RSVP_Setting, _Event }) 
              .Where(x => x._Event.Active == true 
                 && x._Event.ID == _EventID) 
              .Select(x => new 
              { 

               RSVP_ButtonLocations = x._RSVP_ButtonLocation_RSVP_Setting._RSVP_ButtonLocation.RSVP_ButtonLocation 

              }); 

       return _RSVP_ButtonLocations.FirstOrDefault().RSVP_ButtonLocations; 
      } 

但問題是,LINQ查詢不允許我字符串轉換爲GUID值。 任何人都可以給我建議嗎?

+0

怎麼樣,而不是「Guid.Parse(_RSVP_ButtonLocation「新的GUID(_RSVP_ButtonLocation.ID)」。 ID)」 ? – CjCoax

+0

@CjCoax,我按照你的建議更新,我得到這個消息「System.NotSupportedException:LINQ to Entities只支持無參數的構造函數和初始值設定項。」 「 –

+1

@Frank,爲什麼用於連接的數據類型有所不同?也許最好是在兩個表中都有'Guid'?如果不是,可能你可以嘗試進行連接:將Guid轉換爲字符串,所以連接字符串值... – horgh

回答

0

大廈CjCoax評論,你可以存儲GUID的一個實例,然後在後面的查詢中使用它:

using (RMPortalEntities _RMPortalEntities = new RMPortalEntities()) { 
      var GUID = new Guid(_RMPortalEntities.tbl_RSVP_ButtonLocation.ID); 
      var _RSVP_ButtonLocations = _RMPortalEntities 
             .tbl_RSVP_ButtonLocation 
             .Join(_RMPortalEntities.tbl_RSVP_Setting, 
                 _RSVP_ButtonLocation => GUID, 
                 _RSVP_Setting => _RSVP_Setting.RSVP_Button_Location_ID, 
                 (_RSVP_ButtonLocation, _RSVP_Setting) => new { _RSVP_ButtonLocation, _RSVP_Setting }) 
             .Join(_RMPortalEntities.tbl_Event, 
                 _RSVP_ButtonLocation_RSVP_Setting => _RSVP_ButtonLocation_RSVP_Setting._RSVP_Setting.EventID, 
                 _Event => _Event.ID, 
                 (_RSVP_ButtonLocation_RSVP_Setting, _Event) => new { _RSVP_ButtonLocation_RSVP_Setting, _Event }) 
             .Where(x => x._Event.Active == true 
                && x._Event.ID == _EventID) 
             .Select(x => new 
             { 

              RSVP_ButtonLocations = x._RSVP_ButtonLocation_RSVP_Setting._RSVP_ButtonLocation.RSVP_ButtonLocation 

             }); 

      return _RSVP_ButtonLocations.FirstOrDefault().RSVP_ButtonLocations; 
     } 
+0

'var GUID = new Guid(_RSVP_ButtonLocation.ID);'因爲_RSVP_ButtonLocation聲明還沒有出現,所以我怎麼能使用這個語句? –

+0

如果我理解正確,編輯應該是你需要的 –

+0

我感謝你的幫助@Sidharth Mudgal –