How to eliminate and ignore an item from a Collection List(based on Condition) in c#.


Today i want to discuss an article to remove/eliminate an item in a collection list based on some condition.What are the ways to eliminate/remove or ignore an item on a collection list in c# ?This i want to discuss here with code snippets and real time images.

When you are working in a project especially with collection list there might be a scenario you need to remove or even ignore an item in the collection list based on some condition so there are many ways/Methods to ignore or Eliminate/Remove an item from the collection list.Here are the few methods/ways to eliminate or even Ignore an item from the collection list.

Ignoring an Item in the collection list based on condition


Checking each and every item in the collection list and ignore the Item based on a condition :

This is a simple one Let us see how this works in .Net C#.

see the below Line of Code


public double GetActiveCartGrandTotal()
{
LoginDAL ld = new LoginDAL();
List cdCollection =null;
try
{
//if (ld.IsLoggedOn())
//{
Hashtable htCartInfo = ld.GetCartInfo();


if (htCartInfo != null || cdCollection !=null)
{
if (htCartInfo.ContainsKey("CART_DATA") || cdCollection !=null)
{
double _grandTotal = 0.0;
if (cdCollection == null)
{
cdCollection = (List)htCartInfo["CART_DATA"];
}
foreach (CartDetails cd in cdCollection)
{

if (cd.Note == "")
{
_grandTotal += cd.TotalItemPrice;
}

}
return _grandTotal;
}
else
{
return 0.0;
}
}
else
{
cdCollection = (List)htCartInfo["CART_DATA"];
return 0.0;
}
//}
//else
//{
// return 0.0;
//}
}
catch (Exception ex)
{
throw ex;
}
finally
{
ld = null;
}
}





See the above Function is return a Grand Total which is a Double value.But what here is i do n't want to add the price of the product to the Grand Total whose Attribute Note is Back order.

I will explain here with Images in Visual studio .Net run time in Debug Mode so that you will know what exactly happening here.

If Collection List has a cart Data..

Cart Data Image

In the above Image/Screen shot the Collection List has a Cart Data i am allowing each individual product price added to the Grand Total.

Back order new


Now in the Above Image i have a collection List in that One Attribute /Property having Note is Backorder. So I do n't want add that Back order Product Price to the Grand Total.

backorder grandtotal

If i added these Collection List to a Grid.You see the Output. Now see the Below Image the Note is equal to Back order Price is not added to the Grand Total which is beneath of the Grid Footer

Price Added

Here in the below code I want to remove a Particular Item from the Collection List.So i chose to iterate with for loop.

Remove an Item from the Collection List





CartDetailsManager cdm = new CartDetailsManager();
List cdCollectionPaypal = cdm.GetCartDetails();
for (int i = 0; i < cdCollectionPaypal.Count(); i++)
{

if (cdCollectionPaypal[i].Note != "")
{
cdCollectionPaypal.RemoveAt(i);
}
}



In Debug Mode in run time the below snap shot will give a better idea.

See here in the below image you see i had remove the Item from the Collection List.If the attribute/Property is a BackOrder.

RemoveAt(int i) is a method to remove an Particular item (Index/position of that Item)in the Collection List.

RemoveBackorder


Attachments

Article by srirama
A Good advice from parent to a Child , Master to a Student , Scholar to an Ignorant is like a doctor prescribed pill it is bitter to take but when they take it will do all good for them --- Bhushan

Follow srirama or read 74 articles authored by srirama

Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: