List to CSV

S

shapper

Hello,

I have a Linq query which returns items of

List<Tag> Tags = new List<Tag> (from t in database.Tags
where
t.Category = MyCategory).ToList();

Each tag has three properties: ID, Name and Category

I need to convert this list to a CSV string of the Tag names in the
list. How can I do this?

I have done before the conversion from CSV to List:
List<Tag> Tags = MyCSVTags.Split(',').Select(t => new Tag { Name =
t.Trim() }).ToList();

But I am having problems in the List > CSV conversion.

Could someone, please, help me out?

Thanks,
Miguel
 
M

Martin Honnen

shapper said:
I have a Linq query which returns items of

List<Tag> Tags = new List<Tag> (from t in database.Tags
where
t.Category = MyCategory).ToList();

Each tag has three properties: ID, Name and Category

I need to convert this list to a CSV string of the Tag names in the
list. How can I do this?

string s = Tags.Aggregate("", (s, t) => s == "" ? t.Name : s + "," +
t.Name);
 
I

Ignacio Machin ( .NET/ C# MVP )

   string s = Tags.Aggregate("", (s, t) => s == "" ? t.Name :s + "," +
t.Name);

It's a little more complex than that, you have to check if the field
contains a , if so surround it with ", but then you have to check if
the field contains a " and escape it :)
 
S

shapper

It's a little more complex than that, you have to check if the field
contains a , if so surround it with ", but then you have to check if
the field contains a " and escape it :)

What? :)

Could you explain what do you mean? I am completly lost ...
 

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


Top