[英]List.clear() followed by List.add() not working
I have the following C# Class/Function: 我有以下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);
}
}
On a card Game. 在纸牌游戏上。 This works fine, however, I had trouble at first, instead of using myCards = new List();
这样做很好,但是起初我遇到麻烦,而不是使用myCards = new List();。 to 'reset' myCards, I would use myCards.clear(), however, once I called the clear function, I would not be able to call myCards.add() or myCards.addRange().
要“重置” myCard,我将使用myCards.clear(),但是,一旦我调用了clear函数,便无法调用myCards.add()或myCards.addRange()。 The count would stay at zero.
计数将保持为零。 Is my current approach good?
我目前的方法好吗? Is using LINQ to sort my cards good/bad?
使用LINQ对我的卡进行排序是好是坏?
this would work 这会工作
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();
}
}
The problem is that an IEnumerable
is a query, not a list. 问题是
IEnumerable
是查询而不是列表。 Since it's selecting from myCards
, if you clear myCards
before actually executing the query, it'll return no results. 由于它是从
myCards
选择的, myCards
如果您在实际执行查询之前清除myCards
,它将不会返回任何结果。 You can run the query before clearing the list by using IEnumerable.ToList()
thus: 您可以在使用
IEnumerable.ToList()
清除列表之前运行查询,因此:
public void sortBySuitValue()
{
var query = (from s in myCards
orderby (int)s.suit, (int)s.value
select s).ToList();
myCards.Clear();
myCards.AddRange(query);
}
You don't need LINQ to sort, the List class already provides a Sort method which you can pass a Comparison
delegate suitable for you. 您不需要LINQ进行排序,List类已经提供了Sort方法,您可以传递适合您的
Comparison
委托。 Example for sorting strings by length: 按长度对字符串排序的示例:
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;
});
To myCards
is filled with information that you get with the sentence LINQ need to Call some method such as .ToList()
If you do not myCards will contain nothing. 在
myCards
中充满了您从句子LINQ获得的信息,需要调用某些方法,例如.ToList()
如果不这样做,myCards将不包含任何内容。
This should work: 这应该工作:
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();
}
You can use LINQ to generate a query to create a new list (or repopulate a cleared list), but I'd rather use the Sort
instance method of List<T>
to go ahead and sort my existing list in place in this particular situation. 您可以使用LINQ生成查询以创建新列表(或重新填充已清除的列表),但是在这种特殊情况下,我宁愿使用
List<T>
的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);
You can sort the data inplace in the list: 您可以在列表中对数据进行排序:
myCards.Sort( (l,r) =>
l.suit.Equals(r.suit) ? l.value.CompareTo(r.value) : l.suit.CompareTo(r.suit));
This way you won't have to create a new list which is assigned to your old variable. 这样,您将不必创建分配给旧变量的新列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.