Linq 左连接帮助
有人可以帮我解决这个 linq 查询吗?我基本上希望所有玩家都在查询中返回,无论他们是否在评级表中。如果他们确实有评级显示在查询中。此查询的问题在于,如果法官 x 输入了玩家 a 的评分,然后使用法官 y 进行查询,您将不会在结果中得到玩家 a。我想我明白为什么,但可以弄清楚语法。
var players =
from p in dc.Players
join r in dc.Ratings on p.PlayerId equals r.PlayerId into ps
from r in ps.DefaultIfEmpty()
where r.JudgeId == Convert.ToInt32(JudgeId) || r.JudgeId == null
orderby p.PlayerName
select new
{
PlayerName = p.PlayerName,
PlayerId = p.PlayerId,
Passing = r != null ? r.Passing : 0,
Setting = r != null ? r.Setting : 0,
Serving = r != null ? r.Serving : 0,
Blocking = r != null ? r.Blocking : 0,
Effort = r != null ? r.Effort : 0,
Quickness = r != null ? r.Quickness : 0,
Hitting = r != null ? r.Hitting : 0,
PositionKnowledge = r != null ? r.PositionKnowledge : 0,
Total = r != null ? Convert.ToString(r.Passing + r.Setting + r.Serving + r.Blocking + r.Effort + r.Quickness + r.Hitting + r.PositionKnowledge): "You have not rated this person",
};
Can someone help me out with this linq query, I basically want all the players to be returned in the query regardless if they are in the ratings table. If they do have ratings show in the query. The problem with this query is that if judge x entered a rating for player a then querying with judge y you will not get player a in the results. I think i understand why but can figure out the syntax.
var players =
from p in dc.Players
join r in dc.Ratings on p.PlayerId equals r.PlayerId into ps
from r in ps.DefaultIfEmpty()
where r.JudgeId == Convert.ToInt32(JudgeId) || r.JudgeId == null
orderby p.PlayerName
select new
{
PlayerName = p.PlayerName,
PlayerId = p.PlayerId,
Passing = r != null ? r.Passing : 0,
Setting = r != null ? r.Setting : 0,
Serving = r != null ? r.Serving : 0,
Blocking = r != null ? r.Blocking : 0,
Effort = r != null ? r.Effort : 0,
Quickness = r != null ? r.Quickness : 0,
Hitting = r != null ? r.Hitting : 0,
PositionKnowledge = r != null ? r.PositionKnowledge : 0,
Total = r != null ? Convert.ToString(r.Passing + r.Setting + r.Serving + r.Blocking + r.Effort + r.Quickness + r.Hitting + r.PositionKnowledge): "You have not rated this person",
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
迈克尔的想法是正确的。我在 LINQPad 中工作,发现在多个条件下连接的语法比使用 && 稍微复杂一些。不过,运营商。
Michael has the right idea. I was working in LINQPad and found that the syntax to join on multiple conditions is a little more complex than using the && operator, though.
我相信你只需要将你的 where 子句移到连接中,这样就可以代替:
“给我所有玩家,以及他们拥有的任何评级,然后过滤这些评级,而不是通过判断”,
你会得到
“给我所有玩家,并且评委对他们的任何评分”。
I believe you just need to move your where clause into the join, so that instead of this:
"Give me all players, and any ratings they have, then filter those ratings not by judge a"
you get
"Give me all players, and any ratings they have by judge a".
这两个建议不太有效,并且在 Visual Studio 中生成了编译错误。但他们确实激发了一个想法,而且这似乎是可行的。
Those two suggestions didn't quite work and generated compile errors in visual studio. But they did spark an idea and this is what seems to work.