LINQ to Entities无法识别方法System.String ToBase64String(Byte [])

时间:2017-04-05 12:58:13

标签: c# entity-framework linq azure-sql-database

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 to Entities无法识别方法'System.String ToBase64String(Byte [])'方法,因此我得到异常方法无法转换为商店表达式。 我使用sql Azure作为我的后端存储。

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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();