List

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
 
G

Göran Andersson

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());
}
 
S

shapper

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()));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Linq. Select 3
Linq. Please, need help. 1
Filter 1
Condition 2
Linq. Aggregate 4
(Collection) 1
ToString and FromString 10
Linq Query. Please, help. Going crazy ... 1

Top