2012-03-28 57 views
4

我在创建使用LINQ &下面的SQL语句的问题嗨C#如何使联接使用LINQ to SQL和C#

select c.IDAddenda, c.Descripcion 
     from CatAddendas c 
right join EmpresaAddenda e on e.IDAddenda = c.IDAddenda 
    where e.rfc = 'SUL010720JN8' 
    order by c.IDAddenda asc 

我得到这个

public IEnumerable<CatAddenda> TraeAddendas(string rfc) 
{ 
    DataClasses1DataContext dc = new DataClasses1DataContext(...); 

    return (from adds in dc.EmpresaAddendas 
      cats.IDAddenda into joined 
      where adds.RFC == rfc 
      select adds.CatAddenda); 
} 

这是一个正确的没有做正确的加入,所以有什么想法?

+0

http://stackoverflow.com/questions/2730810/right-outer-join-in-linq – Phil 2012-03-28 19:52:26

回答

9
var RightJoin = from adds in dc.EmpresaAddendas 
       join cats in CatAddendas 
        on adds.IDAddenda equals cats.IDAddenda into joined 
       from cats in joined.DefaultIfEmpty() 
       select new 
       { 
        Id = cats.IDAddenda, 
        Description = cats.Descripcion 
       }; 
4
var results = from e in EmpresaAddenda 
       join c in CatAddendas 
       on e.IDAddenda equals c.IDAddenda into f 
       from c in f.DefaultIfEmpty() 
       select new 
       { 
        ID = c.IDAddenda, 
        Description = c.Descripcion 
       }; 

你可以申请WHERE和ORDER BY对结果。