Introduction
Working with data structures is
interesting when it comes to conversion according to our needs some kind of
generic functionality ties hands with us throughout the entire project. So we
need to be more focused on implementing such kind of functionalities. In this
article, I will walk through the conversion of the list to CSV string using c#.
Let us look into this
Example use case
Let us consider this example, we are
having employees list object for which we should do the conversion to a CSV
string using c# without string.join method
var employees = new List
{ new Employee {Id="1", Name="Sekar" , Address="Valapady,Salem" } , new Employee {Id="2", Name="Tamil" , Address="Mangalapuram" }, new Employee {Id="3", Name="Iniyan" , Address="Belur" }, new Employee {Id="4", Name="Vijai" , Address="Vadukathampatty" }, new Employee {Id="5", Name="Baskar" , Address="Palaniyapuram" } };
Example Output
The first row of the output denotes headers for CSV and forthcoming rows are data for CSV. All the values should be delimited with a comma.
2. Let sb is a StringBuilder object
3. Get string[] propertyNames from reflection
4. Iterate : propertyNames until EOA(end of the array), If not EOA goto 4.1 else goto step 5
4.1. If isFirstIteration =true goto step 4.2 else
4.2. Iterate : propertyNames until EOA(end of the array), if not EOA goto 4.3 else goto 4.5
4.3. Get prop=propertyNames(index)
4.4. sb.Append( prop + “,” )
4.5. sb.Remove(sb.Length - 1, 1) Remove last comma
4.6. sb.Append("\r\n") Set new line
4.7. Set isFirstIteration =false
4.8. Get property value for current property through reflection
4.9. Sb.Append( value + “,”)
5. sb.Remove(sb.Length - 1, 1) Remove last comma
6. sb.Append("\r\n") Set new line
7. Stop
Problem Statement
We don’t have an inbuild extension method or any kind of functionality to achieve the expected output. I would like to do a little workaround to reach our goal using c#.Approach
Conversion Algorithm
1. Let bool isFirstIteration =true2. Let sb is a StringBuilder object
3. Get string[] propertyNames from reflection
4. Iterate : propertyNames until EOA(end of the array), If not EOA goto 4.1 else goto step 5
4.1. If isFirstIteration =true goto step 4.2 else
4.2. Iterate : propertyNames until EOA(end of the array), if not EOA goto 4.3 else goto 4.5
4.3. Get prop=propertyNames(index)
4.4. sb.Append( prop + “,” )
4.5. sb.Remove(sb.Length - 1, 1) Remove last comma
4.6. sb.Append("\r\n") Set new line
4.7. Set isFirstIteration =false
4.8. Get property value for current property through reflection
4.9. Sb.Append( value + “,”)
5. sb.Remove(sb.Length - 1, 1) Remove last comma
6. sb.Append("\r\n") Set new line
7. Stop
How to compute its time complexity?
1. Let bool isFirstIteration =true =>O(1)2. Let sb is a StringBuilder object =>O(1)
3. Get string[] propertyNames from reflection => O(1)
4. Iterate : propertyNames until EOA(end of the array), If not EOA goto 4.1 else goto step 5 => O(N)
4.1. If isFirstIteration =true goto step 4.2 else => O(1)
4.2. Iterate : propertyNames until EOA(end of the array), if not EOA goto 4.3 else goto 4.5 => O(N)
4.3. Get prop=propertyNames(index) =>O(1)
4.4. sb.Append( prop + “,” ) => O(1)
4.5. sb.Remove(sb.Length - 1, 1) Remove last comma => O(1)
4.6. sb.Append("\r\n") Set new line => O(1)
4.7. Set isFirstIteration =false => O(1)
4.8. Get property value for current property through reflection => O(1)
4.9. Sb.Append( value + “,”) => O(1)
5. sb.Remove(sb.Length - 1, 1) Remove last comma => O(1)
6. sb.Append("\r\n") Set new line => O(1)
7. Stop
=>{ O(1) + O(1) + O(1) + O(N)+O(1) + O(N) +O(1)+O(1)+ O(1)+ O(1)+ O(1)+ O(1)+ O(1)+ O(1)+ O(1)}
=>{O(13)+O(N)+O(N)}
=>{O(13)+O(2N)}
=>{O(13+2N)}
=>O(N) linear time complexity
What is linear time complexity?
It means the algorithm computational time will depends on input size . In short longer the input size will take more time to compute.
Convert List to CSV String
Based on the conversion algorithm , I implemented the c# code
static string ToCSV( List items)
{
if (items.Count > 0)
{
bool isFirstIteration = true;
StringBuilder sb = new StringBuilder();
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (T item in items)
{
string[] propertyNames = item.GetType().GetProperties().Select(p => p.Name).ToArray();
foreach (var prop in propertyNames)
{
if (isFirstIteration == true)
{
for (int j = 0; j < propertyNames.Length; j++)
{
sb.Append("\"" + propertyNames[j] + "\"" + ',');
}
sb.Remove(sb.Length - 1, 1);
sb.Append("\r\n");
isFirstIteration = false;
}
object propValue = item.GetType().GetProperty(prop).GetValue(item, null);
sb.Append("\"" + propValue + "\"" + ",");
}
sb.Remove(sb.Length - 1, 1);
sb.Append("\r\n");
}
return sb.ToString();
}
else
{
return string.Empty;
}
}
I have wrote this code in generic way and so this can work with any kind of complex object collections.
Full Implementation
Employee.cs
public class Employee
{
public string Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
class Program
{
static void Main(string[] args)
{
var csvString = ToCSV(GetEmployees());
Console.WriteLine(csvString);
Console.ReadKey();
}
static List GetEmployees()
{
var employees = new List {
new Employee {Id="1", Name="Sekar" , Address="Valapady,Salem" } ,
new Employee {Id="2", Name="Tamil" , Address="Mangalapuram" },
new Employee {Id="3", Name="Iniyan" , Address="Belur" },
new Employee {Id="4", Name="Vijai" , Address="Vadukathampatty" },
new Employee {Id="5", Name="Baskar" , Address="Palaniyapuram" }
};
return employees;
}
static string ToCSV( List items)
{
if (items.Count > 0)
{
bool isFirstIteration = true;
StringBuilder sb = new StringBuilder();
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (T item in items)
{
string[] propertyNames = item.GetType().GetProperties().Select(p => p.Name).ToArray();
foreach (var prop in propertyNames)
{
if (isFirstIteration == true)
{
for (int j = 0; j < propertyNames.Length; j++)
{
sb.Append("\"" + propertyNames[j] + "\"" + ',');
}
sb.Remove(sb.Length - 1, 1);
sb.Append("\r\n");
isFirstIteration = false;
}
object propValue = item.GetType().GetProperty(prop).GetValue(item, null);
sb.Append("\"" + propValue + "\"" + ",");
}
sb.Remove(sb.Length - 1, 1);
sb.Append("\r\n");
}
return sb.ToString();
}
else
{
return string.Empty;
}
}
}
It was the Best solution for me and worked Thnxs!
ReplyDelete