2014-01-28 34 views
0

我下面LINQ到實體查詢URL編碼查詢

var spSiteUrl = ConfigurationManager.AppSettings["SharePointURL"]; 
var spDocRoot = string.Format(Resources.Global.SharePointDocumentPathRoot, DateTime.Now.Year); 


var Docs = (from s in _espEntities.SignupDocuments 
      join r in _espEntities.RegistrationRequirements 
       on s.DocumentTypeId equals r.Id 
      where s.TaxEntityPlatformId == thisTaxEntity.TaxEntityPlatformId 
      select new NewBpnRegistrationRequestTypeSubmittedDocument() 
      { 

       DocumentType = r.Description, 

       DocumentUrl = spSiteUrl + "/Documents/" + spDocRoot + "/" + 
        thisTaxEntity.TaxEntityPlatformId + "/" + "Registration/" +    
        s.SharepointDocumentName 

      }).ToArray(); 
    if (Docs.Count() != 0) 
    { 
     newBpn.SubmittedDocuments = Docs; 

    } 

我想要做的就是使用HttpUtility.UrlEncode方法上documenturl獲得通過。

請協助

回答

0

由於LINQ到實體不支持此(因爲該方法不能轉換爲SQL),而LINQ to Objects做,你可以嘗試將數據加載到anonymous ojbects,然後一起工作LINQ to Objects

var Docs = (from s in _espEntities.SignupDocuments 
      join r in _espEntities.RegistrationRequirements 
      on s.DocumentTypeId equals r.Id 
      where s.TaxEntityPlatformId == thisTaxEntity.TaxEntityPlatformId 
      select new 
      { 
       DocumentType = r.Description, 
       DocumentUrl = spSiteUrl 
        + "/Documents/" 
        + spDocRoot + "/" 
        + thisTaxEntity.TaxEntityPlatformId 
        + "/Registration/" 
        + s.SharepointDocumentName 
      }) 
      .ToArray() // Load data and continue with linq-to-object 
      .Select (n => new NewBpnRegistrationRequestTypeSubmittedDocument 
       { 
       DocumentType = n.DocumentType, 
       DocumentUrl = HttpUtility.UrlEncode (n.DocumentUrl) 
       }) 
      .ToArray(); 
+0

,它可以像預期的那樣快速地工作。 – user989865

-1
 select new 
        { 

         DocumentType = r.Description, 

         DocumentUrl = spSiteUrl + "/Documents/" + spDocRoot + "/" + 
          thisTaxEntity.TaxEntityPlatformId + "/" + "Registration/" +    
          s.SharepointDocumentName 

        }).ToArray().Select(p=>new  
       NewBpnRegistrationRequestTypeSubmittedDocument{ 
        DocumentType =p.DocumentType, 
        DocumentUrl =HttpUtility.UrlEncode(p.DocumentUrl) 
       }); 
+0

這將失敗,因爲URL編碼不是一個sql方法 – Maess

0

你不能做到這一點,同時呼籲的背景下,因爲SQL沒有對URL編碼功能,所以你需要做一些事情,如以下:

添加以下屬性NewBpnRegistrationRequestTypeSubmittedDocument

TaxEntityPlatformId 
SharepointDocumentName 

然後:

select new NewBpnRegistrationRequestTypeSubmittedDocument() 
     { 

      DocumentType = r.Description, 

      SharepointDocumentName= SharepointDocumentName, 
      TaxEntityPlatformId = TaxEntityPlatformId 

     }).ToArray(); 

然後通過陣列迭代,設置DocumentUrl如下

doc.DocumentUrl = HttpUtility.UrlEncode(spSiteUrl + "/Documents/" + spDocRoot + "/" + 
        doc.TaxEntityPlatformId + "/" + "Registration/" +    
        doc.SharepointDocumentName); 
+0

謝謝Maess的回答。唯一的挑戰是增加一個額外的,因爲這是一個WCF服務公開的類。 – user989865