Habeeb
Habeeb

Reputation: 1040

Delete item from inner collection

Inner Collection

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;
        }
    }
}

Main collection

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;
        }
    }
}

Object of collection

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

Result i need to delete items from inner collection objItemCollection based on objDeleteItems collection.

I expect without using foreach/for loop. Trying to find solution on linq

Thanks in advance..

Upvotes: 1

Views: 192

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236328

Linq (Language-Integrated Query)is for querying, not for modifying (i.e. updating, adding, deleting). Use foreach loop if you want to modify collection.

You can write query which returns set of items without those you want to delete, and then use results of this query instead of your original collection. If you have same instances of ItemsDetails objects in both collections (otherwise you will need to override Equals and GetHashCode of ItemsDetails class), and you don't need duplicates in result:

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

But I would go without query. Simple loop will do the job (overriding of Equals and GetHashCode still required if you don't use same instances of ItemsDetails):

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

You can move this logic to extension method

 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);
 }

Now code will look like:

 objItemCollection.ICollectionItemDetails.RemoveAll(objDeleteItems);

Upvotes: 3

Related Questions