简体   繁体   中英

Can i convert from byte[] to String in Lambda?

Additional information: LINQ to Entities does not recognize the method 'System.String ToString(System.Object)' method, and this method cannot be translated into a store expression.

Ok, "ToString()" cannot be translated into a store expression, and error is clear.

This is my code:

 var narudzbe = db.Narudzbe
                  .Where(x => x.KupacID == id && x.Status == true)
                  .Select(x => new NarudzbeVM()
                  {
                      BrojNarudzbe = x.BrojNarudzbe,
                      Datum = x.Datum,
                      KupacID = x.KupacID,
                      NarudzbaID = x.NarudzbaID,
                      Otkazano = x.Otkazano,
                      Status = x.Status,
                      StavkeNarudzbe = db.NarudzbaStavke
                                         .Where(y => y.NarudzbaID == x.NarudzbaID)
                                         .Select(z => new NarudzbaStavkeVM()
                                         {
                                             Kolicina = z.Kolicina,
                                             NarudzbaID = z.NarudzbaID,
                                             NarudzbaStavkaID = z.NarudzbaStavkaID,
                                             Proizvod = db.Proizvodi
                                                          .Select(t => new ProizvodTest()
                                                          {
                                                              Cijena = t.Cijena,
                                                              ProizvodID = t.ProizvodID,
                                                              JedinicaMjere = t.JediniceMjere.Naziv,
                                                              Naziv = t.Naziv, 
                                                              Sifra = t.Sifra, 
                                                              SlikaThumb = Convert.ToString(t.SlikaThumb)
                                                           })
                                                          .Where(k => k.ProizvodID == z.ProizvodID)
                                                          .FirstOrDefault()
                                         }).ToList()    
                   }).ToList();

I want to convert byte[] to string, since my class accept string for attribut "SlikaThumb". So,

SlikaThumb = Convert.ToString(t.SlikaThumb)

t.SlikaThumb is type of byte[]. Is there way to do it in lambda ?

As you've said, Linq to Entities doesn't recognize .ToString() calls; it doesn't know how to convert these into SQL. However, you can run that in memory; simply resolve the objects (call .ToList() or something) and then perform the select statement on the in-memory objects. It'll be Linq to Objects and that'll be permitted.

Whether that will work for the purpose you intend is a different question but you definitely will be able to call .ToString() on any object in this way.

I guess the best thing you could do is retrieve the object from database as they are, by using ToList() or an equivalent method to actually do the query, then after that, you work on the retrieved list to convert to the objects you want to send to Android. As far as I know, there is no translated method to T-SQL from LinqToEntities to convert a binary field into a Base64 string.

Ok, this helps. As @Casey say:

Linq to Entities doesn't recognize .ToString() calls; it doesn't know how to convert these into SQL. However, you can run that in memory; simply resolve the objects (call .ToList() or something) and then perform the select statement on the in-memory objects. It'll be Linq to Objects and that'll be permitted.

I tried it on my code and it works. What i done ? I first call ToList() method whenever i getting data from database, and then perform the operations.

This code works fine...

 List<NarudzbeVM> narudzbe = db.Narudzbe.Where(x => x.KupacID == id).ToList().
                                        Select(x => new NarudzbeVM()
                                        {
                                            BrojNarudzbe = x.BrojNarudzbe,
                                            Datum = x.Datum,
                                            KupacID = x.KupacID,
                                            NarudzbaID = x.NarudzbaID,
                                            Otkazano = x.Otkazano,
                                            Status = x.Status,
                                            StavkeNarudzbe = db.NarudzbaStavke.Where(y => y.NarudzbaID == x.NarudzbaID).ToList().
                                                             Select(z => new NarudzbaStavkeVM()
                                                             {
                                                                 Kolicina = z.Kolicina,
                                                                 NarudzbaID = z.NarudzbaID,
                                                                 NarudzbaStavkaID = z.NarudzbaStavkaID,
                                                                 ProizvodID = z.ProizvodID,
                                                                 Proizvod = db.Proizvodi.Where(k => k.ProizvodID == z.ProizvodID).ToList().
                                                                             Select(t => new ProizvodTest()
                                                                             {
                                                                                 Cijena = t.Cijena,
                                                                                 ProizvodID = t.ProizvodID,
                                                                                 JedinicaMjere = t.JediniceMjere.Naziv,
                                                                                 VrstaProizvoda = t.VrsteProizvoda.Naziv,
                                                                                 Naziv = t.Naziv,
                                                                                 Sifra = t.Sifra,
                                                                                 SlikaThumb = Convert.ToBase64String(t.SlikaThumb),
                                                                             }).FirstOrDefault()
                                                             }).ToList()

                                        }).ToList();

try with this

string yourString= System.Text.Encoding.Default.GetString(
                System.Text.Encoding.Default.GetBytes(yorbyteArray));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM