1

I want to combine the name of the list in the list. Error Message : LINQ to Entities does not recognize the method 'System.String Join(System.String, System.String[])' method, and this method cannot be translated into a store expression.

dgv.DataSource = this.oFacade.Context.Books
                                         .Include(c => c.Categories)
                                        .Select(c => new dtoObjectDGV
                                        {
                                            ID = c.ID,
                                            Name = c.Name,
                                            CategoryInfo= String.Join(", ", c.Categories.Select(d => d.Name).ToArray())
                                        }).ToList();

result : Name = C# Book CategoryInfo = X Category, Y Category,... bla bla

IAbstract
  • 19,551
  • 15
  • 98
  • 146
Sinan Bay
  • 171
  • 4
  • 12

1 Answers1

3

Join method is not supported in Linq to Entities, I suggest you to project first the info you need and then, using AsEnumerable extension method, you can switch to Linq to Object to use Join method:

DataSource=this.oFacade.Context.Books
                               .Include(c => c.Categories)
                               .Select(c => new 
                                            {
                                               ID = c.ID,
                                               Name = c.Name,
                                               Categories= c.Categories.Select(d => d.Name)
                                             })
                              .AsEnumerable()
                              .Select(c => new dtoObjectDGV
                                            {
                                               ID = c.ID,
                                               Name = c.Name,
                                               CategoryInfo= String.Join(", ", c.Categories)
                                             })
                             .ToList();

If your table doesn't have to many columns then you can consider not to do the first projection:

DataSource=this.oFacade.Context.Books
                               .Include(c => c.Categories)
                               .AsEnumerable()
                               .Select(c => new dtoObjectDGV
                                            {
                                               ID = c.ID,
                                               Name = c.Name,
                                               CategoryInfo= String.Join(", ", c.Categories.Select(d => d.Name).ToArray())
                                             })
                               .ToList();
ocuenca
  • 38,548
  • 11
  • 89
  • 102