在这个带有连接的LINQ查询中使用Min和Max函数?

时间:2012-07-11 01:31:26

标签: c# linq entity-framework linq-to-sql c#-4.0

我正在制作一个LINQ查询,它是多个表的连接。在结果集中,我想在表格中添加一个显示最小和最大日期 [例如(20/3/2012 - 25/4/2012)] 的列。

我想要选择日期的表格结构(这是一个管理多对多关系的表格)如下:

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)

1 个答案:

答案 0 :(得分:0)

好像你在这种情况下加入两次BTW:

“在InvoiceSet.InvoiceID上的BookedAreas中加入BookedAreasSet等于BookedAreasSet.InvoiceID”

你能不能使用?

DateRange=
(Min(BookedAreasSet.LeasedDate) 
+ "-" + 
Max(BookedAreasSet.LeasedDate))