当前位置:编程学习 > C#/ASP.NET >>

泛型怎么根据ID删除?

    private List<ProductItem> _items;

    public void DeleteItem(int productid)
    {
        _items.RemoveAt(productid);//productid是物品编码
        _lastUpdate = DateTime.Now;
    }
而RomoveAt(i)的参数是泛型内部的排序号!怎么根据我productid进行删除?
--------------------编程问答-------------------- Remove () --------------------编程问答-------------------- 你应该用System.Collections.Generic.Dictionary
如果非要用List 的话只能循环了 --------------------编程问答--------------------

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        List<string> dinosaurs = new List<string>();

        Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity);

        dinosaurs.Add("Tyrannosaurus");
        dinosaurs.Add("Amargasaurus");
        dinosaurs.Add("Mamenchisaurus");
        dinosaurs.Add("Deinonychus");
        dinosaurs.Add("Compsognathus");

        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity);
        Console.WriteLine("Count: {0}", dinosaurs.Count);

        Console.WriteLine("\nContains(\"Deinonychus\"): {0}",
            dinosaurs.Contains("Deinonychus"));

        Console.WriteLine("\nInsert(2, \"Compsognathus\")");
        dinosaurs.Insert(2, "Compsognathus");

        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\ndinosaurs[3]: {0}", dinosaurs[3]);

        Console.WriteLine("\nRemove(\"Compsognathus\")");
        dinosaurs.Remove("Compsognathus");

        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }

        dinosaurs.TrimExcess();
        Console.WriteLine("\nTrimExcess()");
        Console.WriteLine("Capacity: {0}", dinosaurs.Capacity);
        Console.WriteLine("Count: {0}", dinosaurs.Count);

        dinosaurs.Clear();
        Console.WriteLine("\nClear()");
        Console.WriteLine("Capacity: {0}", dinosaurs.Capacity);
        Console.WriteLine("Count: {0}", dinosaurs.Count);
    }
}

/* This code example produces the following output:

Capacity: 0

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Deinonychus
Compsognathus

Capacity: 8
Count: 5

Contains("Deinonychus"): True

Insert(2, "Compsognathus")

Tyrannosaurus
Amargasaurus
Compsognathus
Mamenchisaurus
Deinonychus
Compsognathus

dinosaurs[3]: Mamenchisaurus

Remove("Compsognathus")

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Deinonychus
Compsognathus

TrimExcess()
Capacity: 5
Count: 5

Clear()
Capacity: 5
Count: 0
 */
--------------------编程问答-------------------- 顶,楼上的! --------------------编程问答-------------------- 这个是MSDN上的,我看过了,用的是Remove(),我现在要用RemoveAt(),
是否可行,怎么操作? --------------------编程问答--------------------

public void DeleteItem(int productid) 
    { 
        for(int i =0;i<list.Count;i++)
            {
                if (list[i].ID == productid)
                {
                    list.Remove(list[i]);
                    break;
                }
            }
    } 
--------------------编程问答--------------------
引用 5 楼 czyoooo 的回复:
这个是MSDN上的,我看过了,用的是Remove(),我现在要用RemoveAt(), 
是否可行,怎么操作?


RemoveAt的话..就RemoveAt上面例子中的i --------------------编程问答-------------------- lovehongyun:RemoveAt()参数是内部序列号,不是自己可以定义的
我在考虑是否可以用Predicate解决, --------------------编程问答--------------------

public void DeleteItem(int productid)
{
 for(int i=0;i<_items.Length;i++)
  {
    if(_items[i].ProductId = productid)
   {
    _items.RemoveAt(i);
   }
  }
}
--------------------编程问答--------------------
引用 8 楼 czyoooo 的回复:
lovehongyun:RemoveAt()参数是内部序列号,不是自己可以定义的 
我在考虑是否可以用Predicate解决,


RemoveAt()方法移除的是索引,你看下msdn

如果你要是按productid(内部ID)来删除的话.就用6楼.. --------------------编程问答-------------------- 用HashTable做...有Key..
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,