我在SQL中有两个表
分支
ID | BranchID | GameTypeID | flag
1 | 55 | 111 | 1
2 | 16 | 235 | 0
游戏
GameTypeID
123
456
111
235
458
我要实现的目的是,在加载游戏时,它必须检查GameTypeID是否在该ID的分支表中退出并且该标志为1。因此,例如,如果我通过了BranchId 55,则从游戏列表中获得了gametypeid应该删除111,并提供以下列表
Expected Outcome
Games
235
123
456
458
这是我的代码
GameLevel retVal = new GameLevel(GetGames(gameid, isdateroot, startdate, enddate, iscurrent, isdatesort, requestfrom));
var assignments =GetByBranchId(SessionManager.LoggedBranch.BranchIDb => b.Flag);
if (assignments.Any())
{
var gameTypes = assignments.Select(x => x.GameTypeId.ToString()).ToList();
retVal.Rows = retVal.Rows.Where(x => gameTypes.Contains(x.GameTypeID)).ToList();
}
return retVal;
所以上面发生的事情是,当它通过BranchID 55并去检查标志时,问题是当它发现该行而不忽略GametypeID 111时,它忽略了其他所有内容,这就是我得到的结果后退
*current output*
Games
111
我的代码有什么问题?
答案 0 :(得分:1)
我认为我看到了这个问题。我将在这里遍历您的代码,因此,如果我做出错误的假设,则可以更正我的
//1. This line gets an instance of a List<Games> (or something roughly like that)
GameLevel retVal = new GameLevel(GetGames(gameid, isdateroot, startdate, enddate, iscurrent, isdatesort, requestfrom));
//2. This line gets the GameTypeIds for the flagged Branch
var assignments =GetByBranchId(SessionManager.LoggedBranch.BranchIDb => b.Flag);
//3. self explanatory. 'If there is anything in the assignment variable'
if (assignments.Any())
{
//4. Get a list of GameTypeId values from assignments
var gameTypes = assignments.Select(x => x.GameTypeId.ToString()).ToList();
//5. Here's where I think you went wrong
//5. This line gets the Games where gameTypes contains the GameTypeId in retVal.
retVal.Rows = retVal.Rows.Where(x => gameTypes.Contains(x.GameTypeID)).ToList();
}
return retVal;
您想要的是'retVal'中所有不包含在'gameTypes.GameTypeId'中的游戏 而您实际返回的却是相反的。您将返回“ retVal”中所有在“ gameTypes”中具有匹配GameTypeId的游戏
所以...您所要做的与您当前正在做的相反 更改此行
retVal.Rows = retVal.Rows.Where(x => gameTypes.Contains(x.GameTypeID)).ToList();
进入此
retVal.Rows = retVal.Rows.Where(x => !gameTypes.Contains(x.GameTypeID)).ToList();
我所做的就是添加'not'运算符,现在您返回的游戏与标记的GameTypeId不匹配