List to CSV

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    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
  • rossum

    #2
    Re: List to CSV

    On Mon, 25 Aug 2008 05:22:57 -0700 (PDT), shapper <mdmoura@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

    Comment

    • Martin Honnen

      #3
      Re: List to CSV

      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

      Comment

      • Ignacio Machin ( .NET/ C# MVP )

        #4
        Re: List to CSV

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

        Comment

        • shapper

          #5
          Re: List to CSV

          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 ...

          Comment

          • rossum

            #6
            Re: List to CSV

            On Mon, 25 Aug 2008 11:46:15 -0700 (PDT), shapper <mdmoura@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

            Comment

            Working...