Linq 左连接帮助

发布于 2024-11-04 18:02:05 字数 1148 浏览 5 评论 0原文

有人可以帮我解决这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

燕归巢 2024-11-11 18:02:05

迈克尔的想法是正确的。我在 LINQPad 中工作,发现在多个条件下连接的语法比使用 && 稍微复杂一些。不过,运营商。

var judgeId = Convert.ToInt32(JudgeId); // Can't do this inside the anonymous type declartion, so do it first
var players =
        from p in dc.Players
        join r in dc.Ratings on new {p.PlayerId, JudgeId = judgeId } equals new {r.PlayerId, r.JudgeId} into ps
        from r in ps.DefaultIfEmpty()
        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",
        };

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.

var judgeId = Convert.ToInt32(JudgeId); // Can't do this inside the anonymous type declartion, so do it first
var players =
        from p in dc.Players
        join r in dc.Ratings on new {p.PlayerId, JudgeId = judgeId } equals new {r.PlayerId, r.JudgeId} into ps
        from r in ps.DefaultIfEmpty()
        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",
        };
那请放手 2024-11-11 18:02:05

我相信你只需要将你的 where 子句移到连接中,这样就可以代替:

“给我所有玩家,以及他们拥有的任何评级,然后过滤这些评级,而不是通过判断”,

你会得到

“给我所有玩家,并且评委对他们的任何评分”。

var players =
    from p in dc.Players
    join r in dc.Ratings on p.PlayerId equals r.PlayerId && r.JudgeId == Convert.ToInt32(JudgeId) into ps
    from r in ps.DefaultIfEmpty()
    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",
    };

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".

var players =
    from p in dc.Players
    join r in dc.Ratings on p.PlayerId equals r.PlayerId && r.JudgeId == Convert.ToInt32(JudgeId) into ps
    from r in ps.DefaultIfEmpty()
    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",
    };
汹涌人海 2024-11-11 18:02:05

这两个建议不太有效,并且在 Visual Studio 中生成了编译错误。但他们确实激发了一个想法,而且这似乎是可行的。

  int judgeId = Convert.ToInt32(Session["logged"]); 
        var filtered = from r in dc.Ratings
                       where r.JudgeId == judgeId
                       select r;
        var players = from p in dc.Players join
                      r in filtered on p.PlayerId equals r.PlayerId
                      into ps from r in ps.DefaultIfEmpty() 
                      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", }; 

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.

  int judgeId = Convert.ToInt32(Session["logged"]); 
        var filtered = from r in dc.Ratings
                       where r.JudgeId == judgeId
                       select r;
        var players = from p in dc.Players join
                      r in filtered on p.PlayerId equals r.PlayerId
                      into ps from r in ps.DefaultIfEmpty() 
                      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", }; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文