我有一个通用列表
如何删除项目?
EX:
Class Student
{
private number;
public Number
{
get( return number;)
set( number = value;)
}
private name;
public Name
{
get( return name;)
set( name = value;)
}
main()
{
static List<student> = new list<student>();
list.remove...???
}
}
答案 0 :(得分:24)
好吧,没有什么可以删除,因为你的列表是空的(你也没有给它一个标识符,因此你的代码将无法编译)。您可以使用Remove(T item)
或RemoveAt(int index)
分别删除指定索引处的对象或对象(一旦实际包含某些内容)。
受控代码示例:
void Main(...)
{
var list = new List<Student>();
Student s = new Student(...);
list.Add(s);
list.Remove(s); //removes 's' if it is in the list based on the result of the .Equals method
list.RemoveAt(0); //removes the item at index 0, use the first example if possible/appropriate
}
答案 1 :(得分:17)
根据您的意见,我得出结论,您从输入中读取了学生姓名,您需要删除具有指定姓名的学生。
class Student {
public string Name { get; set; }
public int Number { get; set; }
public Student (string name, int number)
{
Name = name;
Number = number;
}
}
var students = new List<Student> {
new Student ("Daniel", 10),
new Student ("Mike", 20),
new Student ("Ashley", 42)
};
var nameToRemove = Console.ReadLine ();
students.RemoveAll (s => s.Name == nameToRemove); // remove by condition
请注意,这将删除所有具有指定姓名的学生。
如果您需要删除按名称找到的第一个学生,请先使用First
方法找到他,然后为该实例调用Remove
:
var firstMatch = students.First (s => s.Name == nameToRemove);
students.Remove (firstMatch);
如果您想要在删除之前确保只有一个学生具有指定名称,请以类似的方式使用Single
:
var onlyMatch = students.Single (s => s.Name == nameToRemove);
students.Remove (onlyMatch);
请注意Single
如果没有一个项与谓词匹配,则会失败。
答案 2 :(得分:3)
List<Student> students = new List<Student>();
students.Add(new Student {StudentId = 1, StudentName = "Bob",});
students.RemoveAt(0); //Removes the 1st element, will crash if there are no entries
或删除已知学生。
//Find a Single Student in the List.
Student s = students.Where(s => s.StudentId == myStudentId).Single();
//Remove that student from the list.
students.Remove(s);
答案 3 :(得分:1)
好吧,你没有给你的名单一个名字,你的一些语法很难。
void main()
{
static List<Student> studentList = new List<Student>();
}
// later
void deleteFromStudents(Student studentToDelete)
{
studentList.Remove(studentToDelete);
}
此处详细介绍了更多删除功能:C# List Remove Methods
答案 4 :(得分:1)
int count = queue.Count;
while(count>0)
{
HttpQueueItem item = queue[0];
/// If post succeeded..
if (snd.IsNotExceedsAcceptedLeadsPerDayLimit(item.DataSaleID, item.AcceptedLeadsPerDayLimit) && snd.PostRecord(item.DataSaleDetailID, item.PostString, item.duplicateCheckHours, item.Username, item.Password, item.successRegex))
{
if (item.WaitTime > 0)
Thread.Sleep(item.WaitTime);
queue.Remove(item);
}
///If Exceeds Accepted leads per day limit by DataSale..
else if (!snd.IsNotExceedsAcceptedLeadsPerDayLimit(item.DataSaleID, item.AcceptedLeadsPerDayLimit))
{
queue.RemoveAll(obj => obj.DataSaleID == item.DataSaleID);
}
/// If Post failed..
else //if (!snd.PostRecord(item.DataSaleDetailID, item.PostString, item.duplicateCheckHours, item.Username, item.Password, item.successRegex))
{
queue.Remove(item);
}
count = queue.Count;
}
答案 5 :(得分:1)
要从网格视图中的通用列表中删除行或记录,只需编写此代码 -
List<Address> people = new List<Address>();
Address t = new Address();
people.RemoveAt(e.RowIndex);
grdShow.EditIndex = -1;
grdShow.DataSource = people;
grdShow.DataBind();
答案 6 :(得分:0)
也许您可以使用Dictionary<string, Student>
代替List<Student>
。
添加学生时,请添加其ID或名称或任何可以唯一标识的名称。然后,您只需拨打myStudents.Remove(myStudentId)
答案 7 :(得分:0)
尝试linq:
var _resultList = list.Where(a=>a.Name != nameToRemove).Select(a=>a);
答案 8 :(得分:0)
我制作了一个包含7张卡的程序,然后随机播放,我希望能够帮助他们
class Program
{
static void Main(string[] args)
{
Random random = new Random();
var cards = new List<string>();
//CARDS VECRTOR
String[] listas = new String[] { "Card 1", "Card 2", "Card 3", "Card 4", "Card 5", "Card 6", "Card 7"};
for (int i = 0; i<= cards.Count; i++)
{
int number = random.Next(0, 7); //Random number 0--->7
for (int j = 0; j <=6; j++)
{
if (cards.Contains(listas[number])) // NO REPEAT SHUFFLE
{
number = random.Next(0, 7); //AGAIN RANDOM
}
else
{
cards.Add(listas[number]); //ADD CARD
}
}
}
Console.WriteLine(" LIST CARDS");
foreach (var card in cards)
{
Console.Write(card + " ,");
}
Console.WriteLine("Total Cards: "+cards.Count);
//REMOVE
for (int k = 0; k <=6; k++)
{
// salmons.RemoveAt(k);
Console.WriteLine("I take the card: "+cards.ElementAt(k));
cards.RemoveAt(k); //REMOVE CARD
cards.Insert(k,"Card Taken"); //REPLACE INDEX
foreach (var card in cards)
{
Console.Write(card + " " + "\n");
}
}
Console.Read(); //just pause
}
}