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,
});
}
你如何導出爲CSV?標準方式是使用[標準貨幣格式代碼](https://msdn.microsoft.com/en-us/library/dwhawy9k(v = vs.110).aspx#Anchor_1)將字段輸出爲字符串。 。但是,如果不知道上下文,則不清楚是在查詢還是其他地方執行該操作。 –
我添加了一些代碼。我使用視圖中的鏈接調用控制器。 – BlowFish