Entity Framework Left Join 我在使用时遇到的错误
我正在编写一个查询,想要获取一个列表中的内容以及另一个列表中的内容,但遇到了此错误。
我的代码在这里,当我输入 join 而不是 left join 时,它工作正常,但我想要的值没有出现,请帮助:D
var orgRolOlanDuyurular = _context.DuyuruOrgRol.Include(x=>x.Duyuru).ThenInclude(l => l.KL_DuyuruTur).Where(l => l.Duyuru.AktifMi);
var tumDuyurular = _context.Duyuru.Include(l => l.KL_DuyuruTur).Where(l => l.AktifMi);
var c = tumDuyurular.LeftJoin(orgRolOlanDuyurular,
tmDuyurular => tmDuyurular.Id,
orgOlanDuyurular => orgOlanDuyurular.DuyuruId,
(tmDuyurular, orgOlanDuyurular) => new DuyuruPaging{Id = tmDuyurular.Id, BaslangicTarihi = tmDuyurular.BaslangicTarihi, BitisTarihi = tmDuyurular.BitisTarihi, DuyuruTurAd = tmDuyurular.KL_DuyuruTur.Ad, Konu = tmDuyurular.Konu });
I was writing a query that I want to get what's in one of my lists and what's in the other, and I encountered this error.
My code is here, when I type join instead of left join, it works fine, but the value I want is not coming, please help :D
var orgRolOlanDuyurular = _context.DuyuruOrgRol.Include(x=>x.Duyuru).ThenInclude(l => l.KL_DuyuruTur).Where(l => l.Duyuru.AktifMi);
var tumDuyurular = _context.Duyuru.Include(l => l.KL_DuyuruTur).Where(l => l.AktifMi);
var c = tumDuyurular.LeftJoin(orgRolOlanDuyurular,
tmDuyurular => tmDuyurular.Id,
orgOlanDuyurular => orgOlanDuyurular.DuyuruId,
(tmDuyurular, orgOlanDuyurular) => new DuyuruPaging{Id = tmDuyurular.Id, BaslangicTarihi = tmDuyurular.BaslangicTarihi, BitisTarihi = tmDuyurular.BitisTarihi, DuyuruTurAd = tmDuyurular.KL_DuyuruTur.Ad, Konu = tmDuyurular.Konu });
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
左外连接是一种连接,其中第一个集合的每个元素都会被返回,而不管它在第二个集合中是否有任何相关元素。您可以使用 LINQ 通过对组联接的结果调用 DefaultIfEmpty 方法来执行左外联接。
没有与 T-SQL 完全相同的语法来使用左连接或右连接。但是,您可以利用类似的方法。
假设您有两个实体。 SalesOrderDetail 和 Product:
和 Product 如下:
现在使用 DefaultIfEmpty() 和 SelectMany() 在 Products 和 Sales 之间执行左连接,如下所示:
如果您希望使用方法语法,您可以按照以下代码获得相同的结果:
现在您可以使用简单的 foreach 循环,如下所示:
有关更多信息,您可以访问 https://learn.microsoft.com/en-我们/dotnet/csharp/linq/perform-left-outer-joins
A left outer join is a join in which each element of the first collection is returned, regardless of whether it has any correlated elements in the second collection. You can use LINQ to perform a left outer join by calling the DefaultIfEmpty method on the results of a group join.
There is no such syntax exactly the same as T-SQL to use Left or Right joins. However, there is a similar approach that you can leverage.
Assuming that you have two Entities. SalesOrderDetail and Product:
And Product as per below:
Now Perform a left join between Products and Sales using DefaultIfEmpty() and SelectMany() as per below:
If you wish to use method syntax, you can achieve the same result as per below code:
Now you can use a simple foreach loop as per below:
For more information, you can visit https://learn.microsoft.com/en-us/dotnet/csharp/linq/perform-left-outer-joins