List to CSV

  • Thread starter Thread starter shapper
  • Start date Start date
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
 
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);
 
   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 :)
 
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

Linq Query. Please, help. Going crazy ... 1
(Collection) 1
Create List in Linq 4
List conversion 2
ToString and FromString 10
Linq > Group 2
Linq. Please, need help. 1
Linq. Select 2

Back
Top