Tools/LinqToSqlExtensions.cs
/// <summary>
/// Creates a *.csv file from an IQueryable query, dumping out the 'simple' properties/fields.
/// </summary>
/// <param name="query">Represents a SELECT query to execute.</param>
/// <param name="fileName">The name of the file to create.</param>
/// <param name="deleteFile">Whether or not to delete the file specified by <paramref name="fileName"/> if it exists.</param>
/// <remarks>
/// <para>If the <paramref name="query"/> contains any properties that are entity sets (i.e. rows from a FK relationship) the values will not be dumped to the file.</para>
/// <para>This method is useful for debugging purposes or when used in other utilities such as LINQPad.</para>
/// </remarks>
Creates a *.csv file from an IQueryable query, dumping out the 'simple' properties/fields.
Creates a * . csv file from an IQueryable query dumping out the ' simple ' properties / fields .
public static void DumpCSV( this IQueryable query, string fileName, bool deleteFile )
		{
			if ( File.Exists( fileName ) && deleteFile )
			{
				File.Delete( fileName );
			}
			using ( var output = new FileStream( fileName, FileMode.CreateNew ) )
			{
				using ( var writer = new StreamWriter( output ) )
				{
					var firstRow = true;
					PropertyInfo[] properties = null;
					FieldInfo[] fields = null;
					Type type = null;
					bool typeIsAnonymous = false;
					foreach ( var r in query )
					{
						if ( type == null )
						{
							type = r.GetType();
							typeIsAnonymous = type.IsAnonymous();
							properties = type.GetProperties();
							fields = type.GetFields();
						}
						var firstCol = true;
						if ( typeIsAnonymous )
						{
							if ( firstRow )
							{
								foreach ( var p in properties )
								{
									if ( !firstCol ) writer.Write( "," );
									else { firstCol = false; }
									writer.Write( p.Name );
								}
								writer.WriteLine();
							}
							firstRow = false;
							firstCol = true;
							foreach ( var p in properties )
							{
								if ( !firstCol ) writer.Write( "," );
								else { firstCol = false; }
								DumpValue( p.GetValue( r, null ), writer );
							}
						}
						else
						{
							if ( firstRow )
							{
								foreach ( var p in fields )
								{
									if ( !firstCol ) writer.Write( "," );
									else { firstCol = false; }
									writer.Write( p.Name );
								}
								writer.WriteLine();
							}
							firstRow = false;
							firstCol = true;
							foreach ( var p in fields )
							{
								if ( !firstCol ) writer.Write( "," );
								else { firstCol = false; }
								DumpValue( p.GetValue( r ), writer );
							}
						}
						writer.WriteLine();
					}
				}
			}
		}
public static void DumpCSV ( this IQueryable query , string fileName , bool deleteFile ) { if ( File . Exists ( fileName ) && deleteFile ) { File . Delete ( fileName ) ; } using ( var output = new FileStream ( fileName , FileMode . CreateNew ) ) { using ( var writer = new StreamWriter ( output ) ) { var firstRow = true ; PropertyInfo [ ] properties = null ; FieldInfo [ ] fields = null ; Type type = null ; bool typeIsAnonymous = false ; foreach ( var r in query ) { if ( type == null ) { type = r . GetType ( ) ; typeIsAnonymous = type . IsAnonymous ( ) ; properties = type . GetProperties ( ) ; fields = type . GetFields ( ) ; } var firstCol = true ; if ( typeIsAnonymous ) { if ( firstRow ) { foreach ( var p in properties ) { if ( ! firstCol ) writer . Write ( " " ) ; else { firstCol = false ; } writer . Write ( p . Name ) ; } writer . WriteLine ( ) ; } firstRow = false ; firstCol = true ; foreach ( var p in properties ) { if ( ! firstCol ) writer . Write ( " " ) ; else { firstCol = false ; } DumpValue ( p . GetValue ( r , null ) , writer ) ; } } else { if ( firstRow ) { foreach ( var p in fields ) { if ( ! firstCol ) writer . Write ( " " ) ; else { firstCol = false ; } writer . Write ( p . Name ) ; } writer . WriteLine ( ) ; } firstRow = false ; firstCol = true ; foreach ( var p in fields ) { if ( ! firstCol ) writer . Write ( " " ) ; else { firstCol = false ; } DumpValue ( p . GetValue ( r ) , writer ) ; } } writer . WriteLine ( ) ; } } } }
Creates a *.csv file from an IQueryable query, dumping out the 'simple' properties/fields.
Creates a * . csv file from an IQueryable query dumping out the ' simple ' properties / fields .
IQueryable
this
fileName
string
The name of the file to create.
The name of the file to create .
deleteFile
bool
Whether or not to delete the file specified by  if it exists.
Whether or not to delete the file specified by if it exists .
query
Represents a SELECT query to execute.
Represents a SELECT query to execute .
remarks
If the  contains any properties that are entity sets  the values will not be dumped to the file.This method is useful for debugging purposes or when used in other utilities such as LINQPad.
If the contains any properties that are entity sets the values will not be dumped to the file . This method is useful for debugging purposes or when used in other utilities such as LINQPad .