2010-02-21 82 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);