2010-02-21 80 views
1

我有以下c#代碼。加入擴展方法的語法

IQueryable<Invoice> query = service.GetInvoices(); 

    query = query.Where(inv => inv.ProjectID == projectid); 
    query = query.Skip(pageIndex * pageSize).Take(pageSize); 

我想使用Join擴展方法將Join添加到「狀態」表。

找不到這個語法的很多例子。

你能幫忙嗎?

加入詳細資料以幫助您編寫代碼。

加入字段Status.ID到Invoice.invStatus

馬爾科姆

回答

2
var query = 
    (
    from i in service.GetInvoices() 
    from s in service.GetStatuses() 
    where i.ProjectID == projectid && 
      s.ID == i.invStatus //"join" condition here 
      //add here any more where clausole you need 
    select i //you still need invoices returned? 
).Skip(pageIndex * pageSize).Take(pageSize); 

使用連接:

var query = 
    (
    from i in service.GetInvoices() 
    join s in service.GetStatuses() on s.ID equals i.invStatus 
    where i.ProjectID == projectid 
      //add here any more where clausole you need 
    select i //you still need invoices returned? 
).Skip(pageIndex * pageSize).Take(pageSize); 

如果我明白你的數據庫結構正確,我會去爲:

var query = 
    (
    from i in service.GetInvoices() 
    from s in i.Status  //get all the statuses associated to this invoice 
    where i.ProjectID == projectid 
      //add here any more where clausole you need 
    select i //you still need invoices returned? 
).Skip(pageIndex * pageSize).Take(pageSize);