url在linq到实体查询中编码

时间:2014-01-28 12:44:31

标签: c# linq-to-entities linq-to-objects

我有以下Linq to实体查询

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方法。

请帮助

3 个答案:

答案 0 :(得分:0)

由于 LINQ to Entities 不支持此功能(因为该方法无法转换为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 ();

答案 1 :(得分:0)

调用上下文时不能执行此操作,因为SQL中没有URL Encode函数,因此您需要执行以下操作:

将以下属性添加到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);

答案 2 :(得分:-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)
                 });
相关问题