2012-07-11 4 views
0

Вот мой запрос LINQ с несколькими объединениями:Как использовать let с этим запросом 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 я хочу получить минимальную арендуемую дату для каждого счета-фактуры.

Я только начал использовать LINQ, поэтому не знаю, как это сделать.

Прошу вас, руководствоваться.

Благодаря

ответ

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, 
    }; 
Смежные вопросы