Krishnarjun Banoth
Krishnarjun Banoth

Reputation: 1580

LINQ to Entities does not recognize the method System.String ToBase64String(Byte[])

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();

While converting byte array into ToBase64String i am getting exception as LINQ to Entities does not recognize the method 'System.String ToBase64String(Byte[])' method, and this method cannot be translated into a store expression. I am using sql Azure as my back-end storage.

Upvotes: 1

Views: 761

Answers (2)

Liviu Boboia
Liviu Boboia

Reputation: 1754

Linq will translate your query into sql and the query generated will contain a call to the Convert.ToBase64String method, of which sql does not know about. You can do a ToList() to load the object from the database into the memory and then apply the Convert.ToBase64String method

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();

Upvotes: 1

jitender
jitender

Reputation: 10429

What about this one

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();

Upvotes: 0

Related Questions