1

My foreach loops through a List, and I need the loop to write itself to a text file in a form like this:

[
  {
    "Platillo": "Pozole",
    "PDV": "Restaurante",
    "Turno": "matutino",
    "PV": "$45.00",
    "1": "1",
    "2": "1"
  },]

My foreach loop fills a html table at the moment.

 foreach (var item in Model)
{
    var total = 0;
    decimal costo = 0;

    for (int i = 1; i <= 31; i++)
    {
        var value = 0;
        if (item.Fecha.Day == i) { value = item.Cantidad; costo = costo + item.Total; }
        total += value;
    }
    <tr>
        <td class="descripcion">@item.Descripcion</td>
        <td class="pdv">@item.Pdv</td>
        <td class="pdv">@item.Rid</td>
        <td>@((costo / (total + 1)).ToString("C"))</td>
        @for (int i = 1; i <= 31; i++)
        {
            var value = 0;
            int month = item.Fecha.Month;
            if (item.Fecha.Day == i) { value = item.Cantidad; }

        }
        <td>@total</td>
        <td>@(((costo / (total + 1)) * total).ToString("C"))</td>

How do I do this in columns, and do that for every item in my foreach statement?

Arturo Martinez
  • 389
  • 7
  • 28
  • If you have objects that match the JSON, then just serialize the list of those objects? – crashmstr Feb 02 '16 at 03:18
  • @crashmstr I made an edit that obliterated changes in an rather unexpected way and I'm in the middle of trying to get it back. Sorry for the confusion. – jdphenix Feb 02 '16 at 03:19
  • i can serialize the list but i do some code to my data in my foreach statement from that collection that why i am looking a way to get my foreach results to a txt rater that the list with the plain data – Arturo Martinez Feb 02 '16 at 03:20
  • "to a txt"... do you want text or do you want JSON? If you want JSON, the simplest way to do so is to model the data in the formats you want and then serialize. – crashmstr Feb 02 '16 at 03:21
  • i want a .txt file i cna get it serializing the list but i want to add and modify the data of the list before adding it to the file liek in my foreach loop thats why im trying to get a way to write the foreach code to a txt in form of columns or something like that – Arturo Martinez Feb 02 '16 at 03:23
  • @ArturoMartinez I apologize - in an attempt to edit and improve your question I ended up reverting to the original revision and going from there. If what I ended up putting here was not your intent, feel free to edit to clarify. – jdphenix Feb 02 '16 at 03:24
  • So you *don't* care about having valid JSON (then why tag...)? If you *do* want valid JSON, create a class that represents the data you want to output to your "txt" file, then transform your data into that class (constructor could be good), then serialize the data and write it to a file. – crashmstr Feb 02 '16 at 03:25
  • i have a valid json, with my json i save the dato to my list but now i need to make a foreach on that list and write them on a file – Arturo Martinez Feb 02 '16 at 03:27

1 Answers1

1

Check out the answer to a very similar question about writing JSON from a list here: https://stackoverflow.com/a/16921677/2939759

Edit: The linked answer suggests usage of Json.Net - here's the code sample provided (thanks to Liam for his great answer):

List<data> _data = new List<data>();
_data.Add(new data()
{
    Id = 1,
    SSN = 2,
    Message = "A Message"
});
string json = JsonConvert.SerializeObject(_data.ToArray());

//write string to file
System.IO.File.WriteAllText (@"D:\path.txt", json);

All you'd really need to do is add the foreach loop to the code sample provided there

Extra Edit: In retrospect (probably quite a bit too late) I think I've misunderstood Arturo's question. My interpretation of it is that there's a bunch of business logic happening in an ASP.NET MVC View, and Arturo wants the same processing to happen in a place where he can write it to a file, which would probably make more sense in the model. The code for it would probably look pretty similar, perhaps like the below. Note that I don't speak Spanish and haven't tried to compile this...

List<data> _data = new List<data>();

foreach (var item in Model)
{
    var total = 0;
    decimal costo = 0;

    for (int i = 1; i <= 31; i++)
    {
        var value = 0;
        if (item.Fecha.Day == i) { value = item.Cantidad; costo = costo + item.Total; }
        total += value;
    }

    _data.Add(new data()
    {
        Descripcion = item.Descripcion,
        Pdv = item.Pdv,
        Rid = item.Rid,
        Costo = ((costo / (total + 1)).ToString("C")),
        Total = total,
        WhateverThisIs = (((costo / (total + 1)) * total).ToString("C"))
    });
}

string json = JsonConvert.SerializeObject(_data.ToArray());
System.IO.File.AppendAllText(@"D:\path.txt", json); //make sure to use append
Community
  • 1
  • 1
Matt Wanchap
  • 841
  • 8
  • 20
  • the foreach loop was deleted in an edit from an admin but is right back up im using the foreach loop to fill a html but i want it to go directly to a file – Arturo Martinez Feb 02 '16 at 03:23