Git Product home page Git Product logo

enumerabletofilestreamresult's Introduction

EnumerableToFileStreamResult

A simple extension method designed to make it easy to convert an enumerable list of class objects to a FileStreamResult... Or put another way quickly serve up the content of a list as a file download.

The FileStreamResult will contain a delimited data set. By default a file is produced containing header rows, using the property names found on the class, and contents matching the value of each property, separated by commas.

The solution is written in .NET CORE 2 using C#.

How to use

TODO: Complete this section properly

TODO: Publish as NuGet package

In short clone the solution and add a project reference to the ToFileStreamResultExtensions project. At present there is actually only one file in the repository that is required for the extension method, so you could also simply copy and paste it's contents and use it as "seed work" in your own project. The extension method code is found under /src/ToFileStreamResultExtensions/EnumerableToFileStreamResultExtension.cs

Once referenced you can follow one of the below examples to get started.

Returning your own list of objects

[HttpGet]
public IActionResult Get()
{
  var book1 = new Book() { Title = "The Mythical Man Month", PageCount = 322 };
  var book2 = new Book() { Title = "What it is to be a 'coder'", PageCount = 0 };
  var books = new List<Book> { book1, book2};

  return books.ToFileStreamResult();
}

A Dapper example

[HttpGet]
public IActionResult Get()
{
  var orderDetails = connection.Query<OrderDetail>(SELECT TOP 5 * FROM OrderDetails;).ToList();

  return orderDetails.ToFileStreamResult();
}

A Dapper example using Dynamics

[HttpGet]
public IActionResult Get()
{
  var orderDetails = connection.Query<dynamic>(SELECT TOP 5 * FROM OrderDetails;).ToList();
  
  var orderData = orderDetails.Select(row => row as IDictionary<string, object>); //Cast DapperRow objects to Dictionary objects
  
  return orderData.ToFileStreamResult();
}

Using Options - Style 1

[HttpGet]
public IActionResult Get()
{
  var book1 = new Book() { Title = "The Mythical Man Month", PageCount = 322 };
  var book2 = new Book() { Title = "What it is to be a \"coder\"", PageCount = 0 };
  var books = new List<Book> { book1, book2};

  return books.ToFileStreamResult(options =>
    {
        options.UseQuotedIdentifiers = true;
        options.UsePropertyNamesAsHeaders = false;
        options.Delimiter = "\t";
        options.EndOfLine = "\r\n"
        options.ContentType = "application\ms-excel";
        options.FileDownloadName = "library-books.tab"
    });
}

Using Options - Style 2

[HttpGet]
public IActionResult Get()
{
    var book1 = new Book() { Title = "The Mythical Man Month", PageCount = 322 };
    var book2 = new Book() { Title = "What it is to be a \"coder\"", PageCount = 0 };
    var books = new List<Book> { book1, book2 };

    var options = new EnumerableExtension.Options()
    {
        Delimiter = ",",
        EndOfLine = "\r\n",
        FileDownloadName = "library.csv",
        UsePropertyNamesAsHeaders = true,
        UseQuotedIdentifiers = true
    };

    return books.ToFileStreamResult(options);
}

enumerabletofilestreamresult's People

Contributors

jadjare avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.