Pregunta

I have very simple entities.

My Article entity:

public class Article
{
    public Article()
    {
        Tags = new HashSet<Tag>();
    }

    public Guid Id { get; set; }
    public string Title { get; set; }
    public string Body { get; set; }

    public virtual ICollection<Tag> Tags { get; set; }
}

Tag entity:

public class Tag
{
    public Guid Id { get; set; }
    public string Title { get; set; }
}

Relation between these entities (many-to-many)

modelBuilder.Entity<Article>()
    .HasMany(a => a.Tags)
    .WithMany()
    .Map(m =>
    {
        m.MapLeftKey("ArticleId");
        m.MapRightKey("TagId");
        m.ToTable("ArticlesTags");
    });

Problem

I want to show articles table with tags separated by whitespace.

My query doesn't work:

var articles = from article in dbContext.KBArticles
               select new ArticlesListModel()
               {
                   Id = article.Id,
                   Title = article.Title,
                   Tags = string.Join(" ", article.Tags.Select(t => t.Title).ToArray<string>())
               };

Web-server show an error 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"

¿Fue útil?

Solución

You can use AsEnumerable or ToList and retrieve the data first from the database and then process it.

var articles = from article in dbContext.KBArticles.AsEnumerable()
               select new ArticlesListModel()
               {
                   Id = article.Id,
                   Title = article.Title,
                   Tags = string.Join(" ", article.Tags.Select(t => t.Title).ToArray<string>())
               };
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top