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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9914623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 11:12:28  来源:igfitidea点击:

how to make a right join using LINQ to SQL & C#

c#sqllinq

提问by franko_camron

Hey guys I'm having a problem creating the following SQL Statement using LINQ & C#

嘿伙计们,我在使用 LINQ 和 C# 创建以下 SQL 语句时遇到问题

    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?

这不是正确的加入,所以有什么想法吗?

采纳答案by ionden

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

回答by Doguhan Uluca

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.

您可以在结果上应用 where 和 order by。