如何使用这个LINQ查询?

时间:2012-07-11 10:26:52

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

这是我的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 Customer_CustomerGroupSet in Customer_CustomerGroup on Contacts_ObjectsSet.ObjectReferenceID equals Customer_CustomerGroupSet.CustomerID
    join CustomerGroupDiscountsSet in CustomerGroupDiscounts on Customer_CustomerGroupSet.CustomerGroupID equals CustomerGroupDiscountsSet.ID
    join InvoiceStatusSet in InvoiceStatus on InvoiceSet.InvoiceStatusID equals InvoiceStatusSet.ID
    where Contacts_ObjectsSet.ObjectReference=="Company" 
//let minDate=(BookedAreaSet.LeasedDate).Min() where BookedAreaSet.InvoiceID=InvoiceSet.InvoiceID
    select new {licensee=(CompanySet.CompanyName),CustomerGroupDiscountsSet.CustomerGroup,AreaSet.Location,InvoiceSet.InvoiceNumber,InvoiceSet.Amount,InvoiceSet.TotalDiscount,InvoiceSet.GST,
    Paid=(InvoiceSet.InvoiceStatusID==2 ? "Paid":"UnPaid"), 
    datePaid=(InvoiceSet.PaymentDate),InvoiceSet.PaymentDate//,miDate



    };

在查询中,我评论了我要添加的内容以及在Select中添加的注释。从BookedArea表我想获得每个invoiceID的最小租赁日期。

我刚刚开始使用LINQ,所以不知道该怎么做。

请指导我。

由于

1 个答案:

答案 0 :(得分:0)

试试这个:

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 Customer_CustomerGroupSet in Customer_CustomerGroup on Contacts_ObjectsSet.ObjectReferenceID equals Customer_CustomerGroupSet.CustomerID
    join CustomerGroupDiscountsSet in CustomerGroupDiscounts on Customer_CustomerGroupSet.CustomerGroupID equals CustomerGroupDiscountsSet.ID
    join InvoiceStatusSet in InvoiceStatus on InvoiceSet.InvoiceStatusID equals InvoiceStatusSet.ID
    where Contacts_ObjectsSet.ObjectReference == "Company"
    join BookedAreaSet2 in BookedAreas on InvoiceSet.InvoiceID equals BookedAreaSet.InvoiceID into BookedAreas2
    let minDate = BookedAreas2.Select(ba2 => ba2.LeasedDate).Min()
    select new
    {
        licensee = CompanySet.CompanyName,
        CustomerGroupDiscountsSet.CustomerGroup,
        AreaSet.Location,
        InvoiceSet.InvoiceNumber,
        InvoiceSet.Amount,
        InvoiceSet.TotalDiscount,
        InvoiceSet.GST,
        Paid = InvoiceSet.InvoiceStatusID == 2 ? "Paid" : "UnPaid", 
        datePaid = InvoiceSet.PaymentDate,
        InvoiceSet.PaymentDate,
        minDate,
    };