如何写LEFT JOIN和LINQ [英] How to write LEFT JOIN ith LINQ
问题描述
你好,
我有3张桌子
参赛者 - 身份证,姓名,类别
比赛 - 同上,名字
参赛者竞赛 - 比赛,比赛,参赛者
如何编写c#LINQ for以下查询?
SELECT Contst.Name,Cat.Category
FROM 参赛者Contst
LEFT JOIN 参赛者竞赛ContstComp
ON Contst.Id = ContstComp.Contestant4
JOIN 类别Cat
ON Contst.Category = Cat.Id
WHERE ContstComp.Competition IS NULL
提前致谢
var query = 来自比赛 in 参赛者
加入 ContstComp $ b $上的参赛者竞赛b contest.ID等于ContstComp.Contestant4 into ContestContestComp
from ccmp in ContestContestComp.DefaultIfEmpty()
join Cat 类别on contest.Category等于Cat.Id
其中 ContstComp.Competition == null
选择 new {Contst.Name,Cat.Category}
加入< identifier>作为对加入结果的引用。它就像SQL中的'AS'关键字一样。在上面的查询中,连接结果集称为ContestContestComp。
DefaultIfEmpty - 如果集合为空,则返回默认实例。
我认为你推荐这篇文章会更好,
http://msdn.microsoft.com/en-us/library/vstudio/bb397895.aspx [ ^ ]
定义LINQ查询在这种情况下,您应该发布用于查询的类。同时,请查看如何:执行左外连接(C#编程指南)中的例外情况 [ ^ ]
Hi there,
I have 3 tables
Contestant - Id, Name, Category
Competition - Id, Name
ContestantsInCompetition - Id, Competition, Contestant
How can I write c# LINQ for the following Query?
SELECT Contst.Name, Cat.Category
FROM Contestant Contst
LEFT JOIN ContestantsInCompetition ContstComp
ON Contst.Id = ContstComp.Contestant4
JOIN Category Cat
ON Contst.Category = Cat.Id
WHERE ContstComp.Competition IS NULL
Thanks in advance
var query=from contest in Contestant join ContstComp in ContestantsInCompetition on contest.ID equals ContstComp.Contestant4 into ContestContestComp from ccmp in ContestContestComp.DefaultIfEmpty() join Cat in Category on contest.Category equals Cat.Id where ContstComp.Competition == null select new {Contst.Name,Cat.Category}
"Into <identifier>" serve as a reference to the results of join. It is just like ‘AS" keyword in SQL. In above query, join result set referred as "ContestContestComp".
"DefaultIfEmpty" - returns a default instance if the collection is empty.
I think it is better you refer this article,
http://msdn.microsoft.com/en-us/library/vstudio/bb397895.aspx[^]
To define the LINQ query for your case, you should post the classes which you use for the query. Meanwhile, have a look at the exmaple in How to: Perform Left Outer Joins (C# Programming Guide)[^]
这篇关于如何写LEFT JOIN和LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!