帮助Linq离开加入

时间:2011-05-03 21:04:32

标签: c# linq left-join

有人可以帮我解决这个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",
        };

3 个答案:

答案 0 :(得分:2)

迈克尔有正确的想法。我在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",
        };

答案 1 :(得分:1)

我相信你只需要将你的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",
    };

答案 2 :(得分:1)

这两个建议在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", };