1
我正在做一個多表連接的LINQ查詢。在結果集中,我想添加一個顯示最小和最大日期的列,並結合[例如(20/3/2012 - 25/4/2012)]。使用最小和最大函數在此LINQ查詢與連接?
表的結構(這是管理多對多關係的表),從那裏我要挑日期如下:
BookedAreaID int
AreaID int
LeasedDate datetime
InvoiceID int
這裏是我的LINQ查詢:
var selectedResults=
from InvoiceSet in Invoices
join BookedAreaSet in BookedAreas on InvoiceSet.InvoiceID equals BookedAreaSet.InvoiceID
join AreaSet in Areas on BookedAreaSet.AreaID equals AreaSet.AreaID
join ContactSet in Contacts on InvoiceSet.ContactID equals ContactSet.ContactID
join Contacts_ObjectsSet in Contacts_Objects on ContactSet.ContactID equals Contacts_ObjectsSet.ContactID
join CompanySet in Companies on Contacts_ObjectsSet.ObjectReferenceID equals CompanySet.CompanyID
join BookedAreasSet in BookedAreas on InvoiceSet.InvoiceID equals BookedAreasSet.InvoiceID
where Contacts_ObjectsSet.ObjectReference=="Company"
select new {InvoiceSet.InvoiceNumber,InvoiceSet.Amount,InvoiceSet.TotalDiscount,InvoiceSet.GST,
InvoiceSet.PaymentDate,InvoiceSet.ShoppingCentreID,BookedAreasSet.BookedAreaID,AreaSet.Name,Paid=(InvoiceSet.PaymentDate==null ? "UnPaid":"Paid"),
licensee=(CompanySet.CompanyName))
};
我要選擇像這個查詢的內容:
DateRange=
(Min(BookedAreasSet.LeasedDate where BookedAreasSet.InvoiceID=InvoiceSet.InvoiceID)
+ "-" +
Max(BookedAreasSet.LeasedDate where BookedAreasSet.InvoiceID=InvoiceSet.InvoiceID)
你可以這樣做: http://stackoverflow.com/questions/2098375/subquery-in-linq-thats-in-the-select-statement-not-the-where-clause – speti43 2012-07-11 12:56:26