Last active
August 29, 2015 14:12
-
-
Save ODataTeam/668d9fa182c3f74f012b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class CsvWriter : ODataWriter | |
{ | |
private CsvOutputContext context; | |
private List<string> headers; | |
public CsvWriter(CsvOutputContext context, IEdmEntityType entityType) | |
{ | |
this.context = context; | |
this.WriteHeader(entityType); | |
} | |
public override void Flush() | |
{ | |
this.context.Flush(); | |
} | |
public override void WriteEnd() | |
{ | |
} | |
public override void WriteStart(ODataFeed feed) | |
{ | |
} | |
public override void WriteStart(ODataEntry entry) | |
{ | |
this.WriteEntry(entry); | |
} | |
private void WriteHeader(IEdmEntityType entityType) | |
{ | |
this.headers = entityType.Properties().Where(p => p.Type.IsPrimitive()).Select(p => p.Name).ToList(); | |
foreach (var header in this.headers) | |
{ | |
this.context.Writer.Write("{0},", header); | |
} | |
this.context.Writer.WriteLine(); | |
} | |
private void WriteEntry(ODataEntry entry) | |
{ | |
foreach (var header in this.headers) | |
{ | |
var property = entry.Properties.SingleOrDefault(p => p.Name == header); | |
if (property != null) | |
{ | |
this.context.Writer.Write(property.Value); | |
} | |
this.context.Writer.Write(","); | |
} | |
this.context.Writer.WriteLine(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment