473,946 Members | 35,021 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

List to CSV

Hello,

I have a Linq query which returns items of

List<TagTags = new List<Tag(from t in database.Tags
where
t.Category = MyCategory).ToL ist();

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<TagTags = 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
Aug 25 '08 #1
5 2913
On Mon, 25 Aug 2008 05:22:57 -0700 (PDT), shapper <md*****@gmail. com>
wrote:
>Hello,

I have a Linq query which returns items of

List<TagTags = new List<Tag(from t in database.Tags
where
t.Category = MyCategory).ToL ist();

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<TagTags = 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
See http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm for all that
you need to know about CSV files.

rossum

Aug 25 '08 #2
shapper wrote:
I have a Linq query which returns items of

List<TagTags = new List<Tag(from t in database.Tags
where
t.Category = MyCategory).ToL ist();

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

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 25 '08 #3
On Aug 25, 9:04*am, Martin Honnen <mahotr...@yaho o.dewrote:
shapper wrote:
I have a Linq query which returns items of
* * *List<TagTags = new List<Tag(from t in database.Tags
* * * * * * * * * * * * * * * * * ** * * * * * * * * * where
t.Category = MyCategory).ToL ist();
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);

--

* * * * Martin Honnen --- MVP XML
* * * *http://JavaScript.FAQTs.com/
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 :)
Aug 25 '08 #4
On Aug 25, 3:46*pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac... @gmail.comwrote :
On Aug 25, 9:04*am, Martin Honnen <mahotr...@yaho o.dewrote:
shapper wrote:
I have a Linq query which returns items of
* * *List<TagTags = new List<Tag(from t in database.Tags
* * * * * * * * * * * * * * * * * * * * * * * * * * * * where
t.Category = MyCategory).ToL ist();
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);
--
* * * * Martin Honnen --- MVP XML
* * * *http://JavaScript.FAQTs.com/

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 ...
Aug 25 '08 #5
On Mon, 25 Aug 2008 11:46:15 -0700 (PDT), shapper <md*****@gmail. com>
wrote:
>On Aug 25, 3:46*pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac.. .@gmail.comwrot e:
>On Aug 25, 9:04*am, Martin Honnen <mahotr...@yaho o.dewrote:
shapper wrote:
I have a Linq query which returns items of
* * *List<TagTags = new List<Tag(from t in database.Tags
* * * * * * * * * * * * * * * * * * * * * * * * * * * * where
t.Category = MyCategory).ToL ist();
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);
--
* * * * Martin Honnen --- MVP XML
* * * *http://JavaScript.FAQTs.com/

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 ...
CSV fields are separated by commas: ','. If one of your fields is a
string containing a comma: 'string, with comma' then you must wrap
that field in double quotes: "string, with comma". If the field also
contains double quotes then you need to escape the double quotes by
doubling them (usually). Hence 'string, comma, "quotes"!' becomes
"string, comma, ""quotes""! "

Commas need escaping so as not to add spurious fields. Quotes need
escaping so as not to confuse whatever parser you are using.

If none of your data fields contain commas then this will not be a
problem.

rossum

Aug 26 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3117
by: massimo | last post by:
Hey, I wrote this program which should take the numbers entered and sort them out. It doesnąt matter what order, if decreasing or increasing. I guess I'm confused in the sorting part. Anyone has any advices?? #include <iostream> using namespace std;
10
15165
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy *enemydata; // Holds information about an enemy (in a game) // Its a double linked list node
24
5827
by: Robin Cole | last post by:
I'd like a code review if anyone has the time. The code implements a basic skip list library for generic use. I use the following header for debug macros: /* public.h - Public declarations and macros */ #ifndef PUBLIC #define PUBLIC
4
3618
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; };   struct pcb *pcb_pointer;
3
2719
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h> #include<stdlib.h> int main(void) {
0
1832
by: drewy2k12 | last post by:
Heres the story, I have to create a doubly linked list for class, and i have no clue on how to do it, i can barely create a single linked list. It has to have both a head and a tail pointer, and each node in the list must contain two pointers, one pointing forward and one pointing backwards. Each node in the list will contain 3 data values: an item ID (string), a quantity (integer) and a price (float). The ID will contain only letters and...
10
6612
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
0
8652
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be anything from a short integer value to a complex struct type, also has a pointer to the next node in the single-linked list. That pointer will be NULL if the end of the single-linked list is encountered. The single-linked list travels only one...
12
4080
by: kalyan | last post by:
Hi, I am using Linux + SysV Shared memory (sorry, but my question is all about offset + pointers and not about linux/IPC) and hence use offset's instead on pointers to store the linked list in the shared memory. I run fedora 9 and gcc 4.2. I am able to insert values in to the list, remove values from the list, but the problem is in traversing the list. Atlease one or 2 list nodes disappear when traversing from the base of the list or...
7
5789
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template classes. One is List1, the other one is ListNode. The codes are below: // test-linked-list.cpp :...
0
10152
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9981
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11557
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11334
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10686
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7421
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4939
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4533
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.