문제

I have the below Linq to entities query

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;

  }

What I want to do is to use the HttpUtility.UrlEncode method on the documenturl been passed.

Kindly assist

도움이 되었습니까?

해결책

Since LINQ to Entities does not support this (because the method cannot be translated to SQL) while LINQ to Objects does, you can try to load your data into anonymous ojbects and then work with 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 ();

다른 팁

You can't do this while calling the context, because there is no URL Encode function in SQL, so you will need to do something like the following:

Add the following properties to NewBpnRegistrationRequestTypeSubmittedDocument

TaxEntityPlatformId
SharepointDocumentName

Then:

select new NewBpnRegistrationRequestTypeSubmittedDocument()
        {

             DocumentType = r.Description,

             SharepointDocumentName= SharepointDocumentName,
             TaxEntityPlatformId = TaxEntityPlatformId

         }).ToArray();

Then iterate through the array, setting the DocumentUrl as follows

doc.DocumentUrl = HttpUtility.UrlEncode(spSiteUrl + "/Documents/" + spDocRoot + "/" +
                   doc.TaxEntityPlatformId + "/" + "Registration/" +              
                   doc.SharepointDocumentName);
        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)
                 });
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top