在LINQ选择结果中添加计算字段?

时间:2012-07-10 06:38:03

标签: c# linq entity-framework

我有一个LINQ查询(使用EF)

基本上我想根据另一列的值在Select结果中添加一列。

我在数据库表中有PaymentDate列,但没有Paid列。如果PaymentDate列中存在空,则还会显示付款为false,如果其中包含某个日期,则表示已付款为真。

这是我的疑问,请指导我如何做到这一点。

 var selectedResults=
    from InvoiceSet in Invoices
    join BookedAreasSet in BookedAreas 
    on InvoiceSet.InvoiceID equals BookedAreasSet.InvoiceID
    join AreaSet in Areas on BookedAreasSet.AreaID equals AreaSet.AreaID
    select new     {InvoiceSet.InvoiceNumber,InvoiceSet.Amount,InvoiceSet.TotalDiscount,InvoiceSet.GST,       InvoiceSet.PaymentDate,InvoiceSet.ShoppingCentreID,BookedAreasSet.BookedAreaID,
AreaSet.Name,Here I want to add calculated value column based on InvoiceSet.PaymentDate value}

1 个答案:

答案 0 :(得分:2)

我认为你应该可以做这样的事情

var selectedResults=
    from InvoiceSet in Invoices
    join BookedAreasSet in BookedAreas 
    on InvoiceSet.InvoiceID equals BookedAreasSet.InvoiceID
    join AreaSet in Areas on BookedAreasSet.AreaID equals AreaSet.AreaID
    select new { InvoiceSet.InvoiceNumber,InvoiceSet.Amount,InvoiceSet.TotalDiscount,InvoiceSet.GST,       
        InvoiceSet.PaymentDate,InvoiceSet.ShoppingCentreID,BookedAreasSet.BookedAreaID,
        AreaSet.Name,Paid = (InvoiceSet.PaymentDate == null) }