从内部集合中删除项目

时间:2014-03-18 07:33:43

标签: c# linq collections

内部收藏

public class ItemsDetails
{
    //Constructor

    public ItemsDetails(int iID, 
                        string sItemName, 
                        Decimal iPrice, 
                        int iQuantity, 
                        int iPostalCode, 
                        bool bisDB,
                        string SImagePath)
    {
        this.iID = iID;
        this.sItemName = sItemName;
        this.iPrice = iPrice;
        this.iquantity = iQuantity;
        this.iPostalCode = iPostalCode;
        this.bisDB = bisDB;
        this.sImagePath = SImagePath;

    }
    public ItemsDetails()
    {
    }


    #region PrivateProperties
    private int iID;
    private string sItemName;
    private Decimal iPrice;
    private int iquantity;
    private int iPostalCode;
    private string sImagePath;
    private int iMasterID;
    #endregion

    //static List<ItemsDetails> itemsDetails = new List<ItemsDetails>();


    #region public Properties
    public bool bisChanged = false;
    public bool bisDB = true;

    #endregion



    public int IMasterID
    {
        get
        {
            return this.iMasterID;
        }
        set
        {
            if (this.iMasterID != value)
                bisChanged = true;
            this.iMasterID = value;
        }
    }

    public bool BisChanged
    {
        get
        {
            return this.bisChanged;
        }
        set
        {
            this.bisChanged = value;
        }
    }

    public int IPostalCode
    {
        get
        {
            return this.iPostalCode;
        }
        set
        {
            if (this.iPostalCode != value)
                bisChanged = true;

            this.iPostalCode = value;
        }
    }

    public int Iquantity
    {
        get
        {
            return this.iquantity;
        }
        set
        {
            if (this.iquantity != value)
                bisChanged = true;
            this.iquantity = value;
        }
    }

    public Decimal IPrice
    {
        get
        {
            return this.iPrice;
        }
        set
        {
            if (this.iPrice != value)
                bisChanged = true;
            this.iPrice = value;
        }
    }

    public string SItemName
    {
        get
        {
            return this.sItemName;
        }
        set
        {
            if (this.sItemName != value)
                bisChanged = true;
            this.sItemName = value;
        }
    }

    public string SImagePath
    {
        get
        {
            return this.sImagePath;
        }
        set
        {
            if (this.sImagePath != value)
                bisChanged = true;
            this.sImagePath = value;
        }
    }
    public int IID // unique id
    {
        get
        {
            return this.iID;
        }
        set
        {
            if (this.iID != value)
                bisChanged = true;
            this.iID = value;
        }
    }
}

主要收藏

public class ItemsMapping
{
    private int itemMasterID;
    private string sCatagoryName;
    private string sImagePath;

    private ICollection<ItemsDetails> iCollectionItemDetails;

    public string SImagePath
    {
        get
        {
            return this.sImagePath;
        }
        set
        {
            this.sImagePath = value;
        }
    }

    public string SCatagoryName
    {
        get
        {
            return this.sCatagoryName;
        }
        set
        {
            this.sCatagoryName = value;
        }
    }

    public ICollection<ItemsDetails> ICollectionItemDetails
    {
        get
        {
            return this.iCollectionItemDetails;
        }
        set
        {
            this.iCollectionItemDetails = value;
        }
    }

    public int ItemMasterID
    {
        get
        {
            return this.itemMasterID;
        }
        set
        {
            this.itemMasterID = value;
        }
    }
}

收集对象

Collection<ItemsMapping> objItemCollection = getdata();// from db
Collection<ItemsDetails> objDeleteItems = itemsToDelete();// from selected items

结果我需要根据objDeleteItems集合从内部集合objItemCollection中删除项目。

我希望不使用foreach / for循环。试图在linq上找到解决方案

提前致谢..

1 个答案:

答案 0 :(得分:3)

Linq(Language-Integrated Query)用于查询,不用于修改(即更新,添加,删除)。如果要修改集合,请使用foreach循环。

您可以编写返回项目集的查询,而不包含您要删除的项目,然后使用此查询的结果而不是原始集合。如果两个集合中都有ItemsDetails个对象的相同实例(否则您需要覆盖Equals类的GetHashCodeItemsDetails),并且不需要重复结果:

 objItemCollection.ICollectionItemDetails = 
         objItemCollection.ICollectionItemDetails.Except(objDeleteItems);

但我会毫无疑问地去。简单循环将完成工作(如果您不使用Equals的相同实例,则仍然需要覆盖GetHashCodeItemsDetails

 foreach(var item in objDeleteItems)
     objItemCollection.ICollectionItemDetails.Remove(item);

您可以将此逻辑移至扩展方法

 public static void RemoveAll<T>(
    this ICollection<T> source, IEnumerable<T> itemsToRemove)
 {
     if (source == null)
         throw new ArgumentNullException("source");

     foreach(var item in itemsToRemove)
         source.Remove(item);
 }

现在代码如下:

 objItemCollection.ICollectionItemDetails.RemoveAll(objDeleteItems);