質問

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