Join扩展方法的语法

时间:2010-02-21 11:18:01

标签: linq

我有以下c#代码。

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

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

我想使用Join扩展方法将“Join”添加到“Status”表。

找不到很多这方面的语法示例。

你能帮忙吗?

加入详细信息以帮助您编写代码。

加入字段是Status.ID到Invoice.invStatus

马尔科姆

1 个答案:

答案 0 :(得分: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);

使用join:

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);