在LINQ中使用Case或if语句?

时间:2012-07-10 06:59:58

标签: c# linq entity-framework

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

我有以下LINQ查询。请指导我如何在此查询中使用case或if语句。

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 use Case/ If statment based on some other column
    };

请指导。

由于

1 个答案:

答案 0 :(得分:5)

您可以这样做,但使用三元运算符?

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, YourColumn = AreaSet.YourColumnName == yourVariable ? "firstResult" : "other result?"}

您可以通过这种方式组合多个条件。

  

AreaSet.YourColumnName == yourVariable? “firstResult”:   AreaSet.YourColumnName == YoursecondVariable? “一些结果”:“一些   其他结果“