2017-04-05 128 views
1
var pList = (from p in db.RTLS_PERSONDTLS 
      where (lsdAts <= p.CREATED_TIME && 
      p.CREATED_TIME <= DateTime.Now) 
      where p.OPERATION_TYPE == 1 

      let pPhotoRow = (from q in db.Cloud_persons_images 
          where q.Image_name == p.PERSON_ID 
          where (lsdAts <= q.Createdtime && q.Createdtime <= DateTime.Now) 
          select q).FirstOrDefault() 

          let pExt = pPhotoRow.Img_ext 
          let photoBytes = pPhotoRow.Person_img 

          Let personPhoto = new PersonPhotoInfo { PDATA = Convert.ToBase64String(photoBytes), PEXT = pExt } 

      select new PersonListInfoDTO 
      { 
       MOB_NO = p.MOBILE_NO, 
       ACTINACT = (int)p.ACTINACT, 
       PHOTO = personPhoto 
      }).AsNoTracking().ToList(); 

雖然轉換字節數組ToBase64String我收到異常作爲LINQ實體無法識別方法「System.String ToBase64String(Byte [])'方法,並且此方法不能轉換爲存儲表達式。我正在使用sql Azure作爲我的後端存儲。LINQ到實體無法識別方法System.String ToBase64String(字節[])

+0

http://stackoverflow.com/search?q=LINQ+to+Entities+ + +不承認+的方法 –

+0

我曾嘗試過很多可能的解決方案,這是在互聯網上提供這種例外,但我沒有得到解決方案,我的問題..感謝您的鏈接 –

+1

解決方案永遠是唯一的和相同的 - 雙投影。首先在匿名類型中獲取原始數據,然後使用'AsEnumerable()'和實際的投影方法來使用任何CLR方法。 –

回答

0

這個怎麼樣

var pList =from item in (from p in db.RTLS_PERSONDTLS 
      where (lsdAts <= p.CREATED_TIME && 
      p.CREATED_TIME <= DateTime.Now) 
      where p.OPERATION_TYPE == 1 

      let pPhotoRow = (from q in db.Cloud_persons_images 
          where q.Image_name == p.PERSON_ID 
          where (lsdAts <= q.Createdtime && q.Createdtime <= DateTime.Now) 
          select q).FirstOrDefault() 
          select new { 
           p.MOBILE_NO, 
           p.ACTINACT, 
           Img_ext= pPhotoRow.Img_ext, 
           photoBytes=pPhotoRow.Person_img 
           }).ToList())       ) 

      select new PersonListInfoDTO 
      { 
       MOB_NO = item.MOBILE_NO, 
       ACTINACT = (int)item.ACTINACT, 
       PHOTO = new PersonPhotoInfo { PDATA = Convert.ToBase64String(item.photoBytes), PEXT = pExt } 
      }).AsNoTracking().ToList(); 
+0

是的,這個想法對我來說很少修改,修改是1.AsNoTracking()在項目集合中被刪除。 2. PEXT = pExt更改爲PEXT = item.Img_ext。現在其工作很好謝謝你 –

+0

很高興幫助你 – jitender

1

的LINQ將會把你的查詢轉換成SQL和生成將包含對Convert.ToBase64String方法,其中的SQL不知道打電話查詢。 可以做一個ToList()來從數據庫中對象加載到存儲器中,然後應用Convert.ToBase64String方法

var pList = (from item in (from p in db.RTLS_PERSONDTLS 
     where (lsdAts <= p.CREATED_TIME && 
     p.CREATED_TIME <= DateTime.Now) 
     where p.OPERATION_TYPE == 1 

     let pPhotoRow = (from q in db.Cloud_persons_images 
         where q.Image_name == p.PERSON_ID 
         where (lsdAts <= q.Createdtime && q.Createdtime <= DateTime.Now) 
         select q).FirstOrDefault()) 
         select new { 
          p.MOBILE_NO, 
          p.ACTINACT, 
          Img_ext= pPhotoRow.Img_ext, 
          photoBytes=pPhotoRow.Person_img 
          }).ToList() 
     .Select(t=> new PersonListInfoDTO 
     { 
      MOB_NO = item.MOBILE_NO, 
      ACTINACT = (int)item.ACTINACT, 
      PHOTO = new PersonPhotoInfo { PDATA = Convert.ToBase64String(item.photoBytes), PEXT = pExt } 
     }).ToList(); 
+0

這個想法也奏效。但我無法將它標記爲答案堆棧溢出不允許我一次接受兩個答案 –

相關問題