Kuzgun
Kuzgun

Reputation: 4737

How to convert string array to string in Linq to entities?

I have the linq query below

var dd = (from dt in d
          select new
          {
              dt.id,
              dt.date,
              dt.customerid,
              dt.customer,
              dt.Description,
              dt.defect,
              dt.address,
              products = string.Join(",", dt.products)
          }).ToList();

string.Join didn't work, the error is

LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object, System.Object)'

I Googled a lot and couldn't find any other solution. What I want to do is, dt.Products is an array and I want to join it all together in a string. Is it possible in Linq to Entities?

Upvotes: 2

Views: 3510

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 149010

You won't be able to do that within the query, but you can do it in memory by calling .AsEnumerable() first. Try this:

var dd = 
    (from dt in d
     select new
     {
         dt.id,
         dt.date,
         dt.customerid,
         dt.customer,
         dt.Description,
         dt.defect,
         dt.address,
         dt.products
     })
    .AsEnumerable()
    .Select(dt => new {
         dt.id,
         dt.date,
         dt.customerid,
         dt.customer,
         dt.Description,
         dt.defect,
         dt.address,
         products = string.Join(",", dt.products)
    }).ToList();

Note, however, if your data set is particularly large, this could cause a noticeable performance impact.

Upvotes: 6

Related Questions