Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# List.clear()后跟List.add()无效_C#_Linq_List - Fatal编程技术网

C# List.clear()后跟List.add()无效

C# List.clear()后跟List.add()无效,c#,linq,list,C#,Linq,List,我有以下C类/功能: class Hand { private List<Card> myCards = new List<Card>(); public void sortBySuitValue() { IEnumerable<Card> query = from s in myCards orderby (int)s.suit,

我有以下C类/功能:

    class Hand
    {

    private List<Card> myCards = new List<Card>();

    public void sortBySuitValue()
    {
        IEnumerable<Card> query = from s in myCards
                                  orderby (int)s.suit, (int)s.value
                                  select s;

        myCards = new List<Card>();
        myCards.AddRange(query);
    }
 }
在纸牌游戏中。这很好,但是,一开始我遇到了麻烦,没有使用myCards=newlist;要“重置”myCards,我将使用myCards.clear,但是,一旦调用clear函数,我将无法调用myCards.add或myCards.addRange。计数将保持在零。我目前的方法好吗?使用LINQ对我的卡进行好/坏排序吗?

这样可以

    class Hand
    {

    private List<Card> myCards = new List<Card>();

    public void sortBySuitValue()
    {
        myCards = (from s in myCards
                                  orderby (int)s.suit, (int)s.value
                                  select s).ToList();
    }
 }
问题在于IEnumerable是一个查询,而不是一个列表。因为它是从myCards中选择的,如果在实际执行查询之前清除myCards,它将不会返回任何结果。您可以使用IEnumerable.ToList在清除列表之前运行查询,因此:


您不需要LINQ进行排序,List类已经提供了一种排序方法,您可以传递一个适合您的比较委托。 按长度对字符串排序的示例:

        List<string> myList = new List<string>( new string[] { "foo", "bar" } );

        myList.Sort((x, y) => 
        {
            if (x.Length == y.Length)
                return 0;
            else if (x.Length < y.Length)
                return 1;
            else return -1; 
        });
您可以使用LINQ生成查询以创建新列表或重新填充已清除的列表,但我更愿意使用list的Sort实例方法继续在这种特定情况下对现有列表进行排序

List<Card> myCards = SomeMethodGeneratingCards();

Comparison<Card> cardComparison = (card1, card2) =>
    {
        int value = card1.Suit.CompareTo(card2.Suit);
        if (value == 0)
            value = card1.Value.CompareTo(card2.Value);

        return value;
    };

myCards.Sort(cardComparison);

您可以对列表中的数据进行排序:

myCards.Sort( (l,r) => 
     l.suit.Equals(r.suit) ? l.value.CompareTo(r.value) : l.suit.CompareTo(r.suit));
这样,您就不必创建一个分配给旧变量的新列表。

到myCards中充满了您从句子中获得的信息,LINQ需要调用一些方法,例如。ToList如果您不这样做,myCards将不包含任何内容

这应该起作用:

    class Hand
    {

    private List<Card> myCards = new List<Card>();

    public void sortBySuitValue()
    {
        myCards = (from s in myCards
                                  orderby (int)s.suit, (int)s.value
                                  select s).ToList();
    }

你能发布不起作用的代码吗?为什么要在方法中再次初始化myCards?@Rosarch,他会对myCards创建一个查询,然后清除列表。由于查询是惰性计算的,因此当他尝试使用AddRange时,查询将根据空列表进行计算。想想对列表写查询->清除列表->评估列表查询。我看不出这有什么问题,只是你可以减少你的代码,但这没有问题。你为什么不接受这个好答案?当然,既然你现在确实有一个列表,你也可以按原样使用它,但希望上面的内容能说明为什么你首先会遇到这个问题。
    class Hand
    {

    private List<Card> myCards = new List<Card>();

    public void sortBySuitValue()
    {
        myCards = (from s in myCards
                                  orderby (int)s.suit, (int)s.value
                                  select s).ToList();
    }