Blog Archives

linq to csv extension method

Can be used to convert list of strongly typed objects to csv output.

    public static class extensionmethods
        #region LinqToCSV
        public static string ToCsv<T>(this IEnumerable<T> items)
            where T : class
            var csvBuilder = new StringBuilder();
            var properties = typeof(T).GetProperties();
            string header = string.Join(",", properties.Select(p => p.Name.ToCsvValue()).ToArray());

            foreach (T item in items)
                string line = string.Join(",", properties.Select(p => p.GetValue(item, null).ToCsvValue()).ToArray());
            return csvBuilder.ToString();

        private static string ToCsvValue<T>(this T item)
            if (item == null) return "\"\"";

            if (item is string)
                return string.Format("\"{0}\"", item.ToString().Replace("\"", "\\\""));
            double dummy;
            if (double.TryParse(item.ToString(), out dummy))
                return string.Format("{0}", item);
            return string.Format("\"{0}\"", item);

With Statement in C# .Net

If you’re stumbling across this article, chances are you may be looking for a replacement for VB .Net’s “with” statement.

Sadly, there is no exact replacement, but there are alternatives which are equally helpful.

Given a basic class “Robot” below:

public class Robot {
public string name {get;set;}
public int age {get;set;}

Variable Declaration

If you were using “With” simply for initial variable declaration so you didn’t have to specify long constructors, you can use the following instead:

public Robot MyRobot = new Robot() {name="Ronnie-5", age=3300};

If you were using “With” to stay within the context of an object while performing a specific task, then there is no direct equivalent, although I would definitely advise you to try “using” if you’re not already.

using (Robot MyRobot = new MyRobot()) {"Ronnie-5";

if ("something") {
//do something
//when finished it will automatically dispose for you

Extension Methods

Another approach, if your concern is code readability and brevity is to begin using some generic class extension methods in your project to help you simplify common tasks. (see example below)

(Kudos to Anay Kamat for clean and reusable code.)

public static class MetaExtensions
	public static void Set(this object obj,params Func<string,object>[] hash){
			foreach(Func<string,object> member in hash){
				var propertyName = member.Method.GetParameters()[0].Name;
				var propertyValue = member(string.Empty);

So you could also alternatively replace “With” by adding the above class extension method and simply doing:

var myrobot = new Robot();
	name=> "Ronnie-5",
	age=> 3300

//or as a one-liner


Now this is technically more lines of code than simply declaring initialization values in the first code snippet, but the upside is… well actually, I’m not sure of the upside, but it’s definitely cool. 🙂

Anay Kamat (blog),
MSDN (Extension Methods),