search inside collection

I have an array with multiple items in it.  I want to pull a value of an item from the array where specific items value is "X"

so here is what I want
Collection Items
         collection.Item(0).StateAlpha = 'AR'
         collection.Item(0).PolicyType = P
         collection.Item(0).HasAClaim = N
         collection.Item(0).HasAClaimThisMod  = N
         collection.Item(0).SearchStatus  = P
         collection.Item(0).Mod = 2

         collection.Item(1).StateAlpha = 'AZ'
         collection.Item(1).PolicyType = P
         collection.Item(1).HasAClaim = N
         collection.Item(1).HasAClaimThisMod  = N
         collection.Item(1).SearchStatus  = P
         collection.Item(1).Mod = 3

and so on ......

this is what i want to do

string x = get me the value from collection where Mod = 3
pratikshahseAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Please show how you define the collection in your program and how the item which is stored in it is defined?
0
pratikshahseAuthor Commented:
my collection is defined in a seperate class. basically I read a value out of DB and populate the collection
0
AndyAinscowFreelance programmer / ConsultantCommented:
If it is your own collection class then you will have to code the search function yourself.  

Consider using one of the collections (eg. map, list...) provided by the .net framework.  They support functions such as Find where you can pass a delegate to a function that can check if the item meets the criteria supplied.  There are examples in the help files.
0
Fernando SotoRetiredCommented:
That did not answer my question. With the info given in the question you can try something like the following.

You can use Linq query if the collection implements IEnumerable. Something like the following.
// Item looking for.
string item = "P";

var results = (from c in collection
               where c.PolicyType == item
               select c).ToList();

Open in new window

The results variable will contain a List of the type in the collection that have its PolicyType set to "P". or no objections in results if none were found.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
it_saigeDeveloperCommented:
Putting a face to Fernando's proposed solution:
using System;
using System.Linq;

namespace EE_Q28736987
{
	class Program
	{
		static Item[] collection;

		static void Main(string[] args)
		{
			collection = new Item[Enum.GetValues(typeof(State)).Length];
			for (int i = 0; i < Enum.GetValues(typeof(State)).Length; i++)
			{
				if (i % 5 == 0)
					collection[i] = new Item() { StateAlpha = String.Format("'{0}'", (State)i), PolicyType = 'P', HasAClaim = i % 2 == 0, HasAClaimThisMod = i % 2 != 0, SearchStatus = 'U', Mod = 5 };
				else if (i % 4 == 0)
					collection[i] = new Item() { StateAlpha = String.Format("'{0}'", (State)i), PolicyType = 'U', HasAClaim = i % 2 != 0, HasAClaimThisMod = i % 2 == 0, SearchStatus = 'P', Mod = 4 };
				else if (i % 3 == 0)
					collection[i] = new Item() { StateAlpha = String.Format("'{0}'", (State)i), PolicyType = 'P', HasAClaim = i % 2 == 0, HasAClaimThisMod = i % 2 == 0, SearchStatus = 'P', Mod = 3 };
				else if (i % 2 == 0)
					collection[i] = new Item() { StateAlpha = String.Format("'{0}'", (State)i), PolicyType = 'U', HasAClaim = i % 2 != 0, HasAClaimThisMod = i % 2 != 0, SearchStatus = 'P', Mod = 2 };
				else if (i % 1 == 0)
					collection[i] = new Item() { StateAlpha = String.Format("'{0}'", (State)i), PolicyType = 'P', HasAClaim = i % 2 != 0, HasAClaimThisMod = i % 2 != 0, SearchStatus = 'U', Mod = 1 };
			}

			var threes = (from item in collection where item.Mod == 3 select item);
			foreach (var item in threes)
				Console.WriteLine(item);

			Console.ReadLine();
		}
	}

	class Item
	{
		public string StateAlpha { get; set;}
		public char PolicyType { get; set; }
		public bool HasAClaim { get; set; }
		public bool HasAClaimThisMod { get; set; }
		public char SearchStatus { get; set; }
		public int Mod { get; set; }

		public override string ToString()
		{
			return string.Format("State: {0}; PolicyType: {1}; SearchStatus: {2}", StateAlpha, PolicyType, SearchStatus);
		}
	}

	public enum State : int
	{
		AL = 0,
		AK,
		AS,
		AZ,
		AR,
		CA,
		CO,
		CT,
		DE,
		DC,
		FM,
		FL,
		GA,
		GU,
		HI,
		ID,
		IL,
		IN,
		IA,
		KS,
		KY,
		LA,
		ME,
		MH,
		MD,
		MA,
		MI,
		MN,
		MS,
		MO,
		MT,
		NE,
		NV,
		NH,
		NJ,
		NM,
		NY,
		NC,
		ND,
		MP,
		OH,
		OK,
		OR,
		PW,
		PA,
		PR,
		RI,
		SC,
		SD,
		TN,
		TX,
		UT,
		VT,
		VI,
		VA,
		WA,
		WV,
		WI,
		WY
	}
}

Open in new window

Produces the following output -Capture.JPG-saige-
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.