如何使用LINQ to SQL和C#进行正确的连接

前端之家收集整理的这篇文章主要介绍了如何使用LINQ to SQL和C#进行正确的连接前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
嘿伙计们,我在使用LINQ&创建以下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);
}

这不是正确的加入,所以任何想法?

解决方法

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

猜你在找的MsSQL相关文章