2016-12-27 45 views
1

我正在使用C#MVC。我有一个使用Linq导出CSV Excel文件的类,我想将一列的格式设置为一个价格,而不是一个小数,可能有无限多的0。是否可以将Linq格式化为价格

编辑

控制器:

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(); 

库:

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

你如何导出为CSV?标准方式是使用[标准货币格式代码](https://msdn.microsoft.com/en-us/library/dwhawy9k(v = vs.110).aspx#Anchor_1)将字段输出为字符串。 。但是,如果不知道上下文,则不清楚是在查询还是其他地方执行该操作。 –

+0

我添加了一些代码。我使用视图中的链接调用控制器。 – BlowFish

回答

1

这是一个有点很难说你的任何结构,没有你发帖任何代码。但是,以下展示了如何变换int为货币字符串:

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

它给我:

kr. 3,00 
kr. 5,00 
kr. 6,00 
kr. 7,00 
相关问题