Click here to Skip to main content
15,909,591 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi all,
i didn't able to convert from list to string in my class.
i.e i have a class in webservice which returns list .
now i want to convert into string.
how can i will do.
pls advice to me,
Thanks in Advance.


here my code:
C#
[WebMethod]
        public List<employeeinformation> GetAllInfor()
        {
            DataClasses1DataContext dbcontext = new DataClasses1DataContext();
            var EMPLOYEEEntity = (from p in dbcontext.EMPLOYEEInformations select p);
            return EMPLOYEEEntity.ToList();
        }


now i want to return string ..
Posted
Updated 28-Aug-12 23:52pm
v2
Comments
AmitGajjar 29-Aug-12 5:54am    
i can't understand why you want to convert List to string? what you want to return ? out of this list what should be converted as a string ?
Zoltán Zörgő 29-Aug-12 5:56am    
A JSON string? A joined string? How do you want to encode the list into the string?

How about this:
http://www.dotnetperls.com/convert-list-string[^]

Code:
C#
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
    List<string> dogs = new List<string>(); // Create new list of strings
    dogs.Add("Aigi"); // Add string 1
    dogs.Add("Spitz"); // 2
    dogs.Add("Mastiff"); // 3
    dogs.Add("Finnish Spitz"); // 4
    dogs.Add("Briard"); // 5

    string dogCsv = string.Join(",", dogs.ToArray());
    Console.WriteLine(dogCsv);
    }
}
 
Share this answer
 
XML
List<string> names = new List<string>() { "John", "Anna", "Monica" };
var result = String.Join(", ", names.ToArray());


or

C#
var names = new List<string>() { "John", "Anna", "Monica" };
var joinedNames = names.Aggregate((a, b) => a + ", " + b);



http://stackoverflow.com/questions/3575029/c-sharp-liststring-to-string-with-delimiter[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900