2010-02-21 5 views
1

J'ai le code C# suivant.Syntaxe pour la méthode d'extension Join

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

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

Je souhaite ajouter une jointure à une table "Status" en utilisant la méthode d'extension Join.

Impossible de trouver plusieurs exemples de syntaxe pour cela.

Pouvez-vous aider?

Joignez-vous aux détails pour vous aider à écrire le code.

champs de jointure sont Status.ID à Invoice.invStatus

Malcolm

Répondre

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

En utilisant rejoindre:

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

Si je comprends bien votre structure db correctement, je pencherais pour:

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