List

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I have a class, Article, with 2 properties: List<Tag> Tags and String
TagsCsv.

I am using the following to fill TagsCsv from Tags list:
MyArticle.TagsCsv = String.Join(", ", MyArticle.Tags.Select(t =>
t.Name).ToArray());

However, I have now a list of articles: List<Article> Articles to
which I want to apply the same rule (Tags to TagsCsv conversion) to
each article on that list.

How can I do this?

And is this possible to do with Linq instead of a loop?

Thanks,
Miguel
 
shapper said:
Hello,

I have a class, Article, with 2 properties: List<Tag> Tags and String
TagsCsv.

I am using the following to fill TagsCsv from Tags list:
MyArticle.TagsCsv = String.Join(", ", MyArticle.Tags.Select(t =>
t.Name).ToArray());

However, I have now a list of articles: List<Article> Articles to
which I want to apply the same rule (Tags to TagsCsv conversion) to
each article on that list.

How can I do this?

And is this possible to do with Linq instead of a loop?

Thanks,
Miguel

As you will be updating each article, there is no point in trying to
avoid a loop. The code will be running a loop either way, and trying to
hide it will only make the code harder to understand.

The loop is pretty straight forward:

foreach (Article article in Articles) {
article.TagsCsv = String.Join(", ", article.Tags.Select(t =>
t.Name).ToArray());
}
 
As you will be updating each article, there is no point in trying to
avoid a loop. The code will be running a loop either way, and trying to
hide it will only make the code harder to understand.

The loop is pretty straight forward:

foreach (Article article in Articles) {
    article.TagsCsv = String.Join(", ", article.Tags.Select(t =>
t.Name).ToArray());

}

And if I am not wrong the Linq would be:

vArticle.Articles.Select(a => a.TagsCsv = String.Join(", ",
a.Tags.Select(ti => ti.Name).ToArray()));
 
Back
Top