2016-12-27 11 views
1

Ich verwende C# MVC. Ich habe eine Klasse, die Linq verwendet, um eine CSV-Excel-Datei zu exportieren, und ich würde gerne eine Spalte als Preis statt nur als Dezimalzahl mit einem möglichen unendlichen Betrag von Nullen formatieren.Ist es möglich, Linq zu einem Preis zu formatieren

EDIT

Controller:

public ActionResult Summary() 
     { 
      _logger.Debug("Request made for File."); 

      var currentYearSetup = _db.GetYearSetupForMostRecentOrCurrentReportingPeriod(); 
      var types = _db.GetAnimalTypes().ToArray(); 
      var rows = _db.GetSummaryRows(); 
      var file = _sm.CreateSummaryFile(rows); 
      var filename = String.Format("Summary_{0}.csv", DateTime.Now.ToString("yyyy_MM_dd__HH_mm")); 
      _logger.DebugFormat("Serving {0}", filename); 
      return File(file, System.Net.Mime.MediaTypeNames.Application.Octet, filename); 
     } 

IRepository:

IEnumerable<SummaryRow> GetSummaryRows(); 

Repository:

public IEnumerable<SummaryRow> GetSummaryRows() 
     { 
      var thing = _db.YearSetups.Select(i => new 
      { 
       Year = i.Name, 
       Transfer = _db.BatchPaymentSplits 
        .Where(bps => bps.YearSetupId == i.YearSetupId) 
        .Where(bps => bps.CustomerIdEntered != null) 
        .Where(bps => _db.BatchPayments 
         .Where(bp => _db.Batches.Where(b => b.BatchTypeId.Equals("T")) 
               .Select(b => b.BatchId) 
               .Contains(bp.BatchId) 
         ) 
         .Select(bp => bp.BatchPaymentId).Contains(bps.BatchPaymentId) 
        ) 

        .Sum(bps => (decimal?)bps.Amount) ?? 0, 
      }); 

      return _db.YearSetups.Where(y => _db.BatchPaymentSplits.Select(bps => bps.YearSetupId).Contains(y.YearSetupId)) 
      .Select(i => new 
      { 
       Year = i.Name, 
       Deposit = _db.BatchPaymentSplits 
        .Where(bps => bps.YearSetupId == i.YearSetupId) 
        .Where(bps => bps.CustomerIdEntered != null) 
        .Where(bps => _db.BatchPayments 
         .Where(bp => _db.Batches.Where(b => b.BatchTypeId.Equals("C") || b.BatchTypeId.Equals("E") || b.BatchTypeId.Equals("M")) 
               .Select(b => b.BatchId) 
               .Contains(bp.BatchId) 
         ) 
         .Select(bp => bp.BatchPaymentId).Contains(bps.BatchPaymentId) 
        ) 
        .Sum(bps => (decimal?)bps.Amount) ?? 0, 
       Transfer = _db.BatchPaymentSplits 
        .Where(bps => bps.YearSetupId == i.YearSetupId) 
        .Where(bps => bps.CustomerIdEntered != null) 
        .Where(bps => _db.BatchPayments 
         .Where(bp => _db.Batches.Where(b => b.BatchTypeId.Equals("T")) 
               .Select(b => b.BatchId) 
               .Contains(bp.BatchId) 
         ) 
         .Select(bp => bp.BatchPaymentId).Contains(bps.BatchPaymentId) 
        ) 

        .Sum(bps => (decimal?)bps.Amount) ?? 0, 
       NSF = _db.BatchPaymentSplits 
        .Where(bps => bps.YearSetupId == i.YearSetupId) 
        .Where(bps => bps.CustomerIdEntered != null) 
        .Where(bps => _db.BatchPayments 
         .Where(bp => _db.Batches.Where(b => b.BatchTypeId.Equals("N")) 
               .Select(b => b.BatchId) 
               .Contains(bp.BatchId) 
         ) 
         .Select(bp => bp.BatchPaymentId).Contains(bps.BatchPaymentId) 
        ) 

        .Sum(bps => (decimal?)bps.Amount) ?? 0, 
       BankCorrection = _db.BatchPaymentSplits 
        .Where(bps => bps.YearSetupId == i.YearSetupId) 
        .Where(bps => bps.CustomerIdEntered != null) 
        .Where(bps => _db.BatchPayments 
         .Where(bp => _db.Batches.Where(b => b.BatchTypeId.Equals("B")) 
               .Select(b => b.BatchId) 
               .Contains(bp.BatchId) 
         ) 
         .Select(bp => bp.BatchPaymentId).Contains(bps.BatchPaymentId) 
        ) 

        .Sum(bps => (decimal?)bps.Amount) ?? 0, 
       Reverse = _db.BatchPaymentSplits 
        .Where(bps => bps.YearSetupId == i.YearSetupId) 
        .Where(bps => bps.CustomerIdEntered != null) 
        .Where(bps => _db.BatchPayments 
         .Where(bp => _db.Batches.Where(b => b.BatchTypeId.Equals("R")) 
               .Select(b => b.BatchId) 
               .Contains(bp.BatchId) 
         ) 
         .Select(bp => bp.BatchPaymentId).Contains(bps.BatchPaymentId) 
        ) 

        .Sum(bps => (decimal?)bps.Amount) ?? 0, 

      }).AsEnumerable().Select(r => new PaymentTransactionSummaryRow() { 
       Year = r.Year, 
       DepositedRaw = r.Deposit, 
       TransferedRaw = r.Transfer, 
       NSFRaw = r.NSF, 
       BankCorrectionRaw = r.BankCorrection, 
       ReversedRaw = r.Reverse, 
      }); 
     } 
+0

Wie exportieren Sie in eine CSV-? Der Standardweg besteht darin, das Feld als Zeichenfolge auszugeben, indem Sie den Standardformat-Währungscode verwenden (https://msdn.microsoft.com/en-us/library/dwhawy9k (v = vs.110) .aspx # Anchor_1). . Aber ohne den Kontext zu kennen, ist unklar, ob das in der Abfrage oder woanders gemacht wird. –

+0

Ich habe etwas Code hinzugefügt. Ich rufe den Controller über einen Link in der Ansicht an. – BlowFish

Antwort

1

Es ist ein bisschen schwer, etwas über Ihre Struktur zu sagen, ohne dass Sie Buchung irgendein Code. Aber die folgenden zeigt, wie Sie int auf einen Währungsstring umwandeln kann:

var intArray = new[] {3,5,6,7}; 
var currencyArray = intArray.Select(x => x.ToString("C")); 

Es gibt mir:

kr. 3,00 
kr. 5,00 
kr. 6,00 
kr. 7,00 
Verwandte Themen