是否可以将Linq格式化为一个价格

时间:2016-12-27 16:58:29

标签: c# linq model-view-controller

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

修改

控制器:

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,
            });
        }

1 个答案:

答案 0 :(得分: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