简体   繁体   中英

How to make a right join using LINQ to SQL & C#?

I have a problem creating the following SQL Statement using LINQ & 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

I got this:

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

This is not doing a right join, so any ideas?

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

You can apply where and order by on the results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM