比较列表以确定List<>中的正确位置

时间:2013-12-16 15:26:01

标签: c# asp.net-mvc asp.net-mvc-4

我有两个需要比较的列表:

var list = new Guess(guesses, 0 , 0);// see class for constructor

theGame.Target = guesses;
var list2 = theGame.Target;

我需要做以下事情:

GuessTheDigits正确设置RightDigitsInTheRightPosition的数量

GuessTheDigits:

public ActionResult GuessTheDigits(List<int> guesses)
{
    GuessingGame theGame = this.Session["GameState"] as GuessingGame;

    // pass to the method
    theGame.GuessTheHiddenDigits(guesses);

    //set the lists
    var list = new Guess();

    theGame.Target = guesses;

    var list2 = theGame.Target;

    //need to compare lists here

    // Redirect to action
    return RedirectToAction("Index", theGame.Guesses);
}

课程如下:

猜类

public class Guess
{
    public Guess()
        : this(new List<int>() { 1, 2, 3 }, 0, 0 )
    {

    }
    public Guess(List<int> guesses, int rdrp, int rdwp)
    {
        this.Digits = guesses;
        this.RightDigitRightPosition = rdrp;
        this.RightDigitWrongPosition = rdwp;
    }

    public Guess(List<int> guesses, int rdrp)
        : this(guesses, rdrp, 0)
    {

    }

    public Guess(List<int> guesses)
       : this(guesses, 0, 0)
    {

    }

    public List<int> Digits { get; set; }
    public int RightDigitRightPosition { get; set; }
    public int RightDigitWrongPosition { get; set; }
}

之前尝试过的代码:

var check = list2.Where(s => !theGame.Guesses.Contains(list));
foreach (var i in check)
{
    list.RightDigitRightPosition = i;
}

2 个答案:

答案 0 :(得分:1)

假设theGame.GuessesList<Guess>,那么我认为(因为它并不完全清楚)您所追求的代码是

var samePositions = myGame.Guesses.Where(x => list.Digits.IndexOf(x.RightDigitRightPosition) == x.Digits.IndexOf(x.RightDigitRightPosition)));

这会提取所有RightDigitRightPosition值且与list位于同一位置的猜测。

答案 1 :(得分:0)

您可以使用IndexOf查找值的位置:

int i = this.Digits.IndexOf(value);

目前还不清楚如何设置 RightDigitRightPositionRightDigitWrongPosition以确切知道如何做到这一点。