Using .Contains() on a property in a list?

Take a look to the LINQ, You can replace with it your code by: ActivityList. Any(i => i. Id == GuidToCompare).

Sure. Foreach(Activity activity in ActivityList. Where(a => a.Id == GuidToCompare) ) { //Code here } But since Id implies there will be at most 1 activity: //var act = ActivityList.

Where(a => a. Id == GuidToCompare).SingleOrDefault(); var act = ActivityList. SingleOrDefault(a => a.

Id == GuidToCompare); // shorted if (act! = null) { //Code here }.

Foreach(var activity in ActivityList. Where(p=>p. Id == GuidToCompare)) { // Code here }.

I Havent tested it but im fairly sure this should work: if ( ActivityList. Any ( a => a. Id == GuidToCompare ) ) { // TODO - Exists.

} MSDN Any : msdn.microsoft.com/en-us/library/bb53497....

Var activity = ActivityList. FirstOrDefault(a => a. Id == GuidToCompare); Or SingleOrDefault if you want to throw an exception in case there is more than 1 match.

Just to offer you all the ways you can write this query with Linq var result = (from activity in activityList where activity. Id == GuidToCompare select activity ).FirstOrDefault(); if(result! = null) /* Code here */ Now, it is up to you to choose the more readable snippet ;).

To find all activity objects with the given GUID you can use: var results = ActivityList. FindAll(item => item. ID == GuidToCompare).

For those who can't use LINQ: List ActivityList = new List(); foreach (Activity activity in ActivityList. FindAll(delegate(Activity a) { return a. Id == GuidToCompare; })) { //Code here }.

If you are looking for only one Id one time, there is no more efficient way. If you are looking for Ids multiple times you can build a HashSet : var activityIdsQuery = from a in ActivityList select a. Id; HashSet activityIds = new HashSet(activityIdsQuery); //Use the hashset activityIds.

Contains(id); If you need to find an instance of activity you can build a Dictionary (works only if Id is unique) : Dictionary activities = ActivityList. ToDictionary(a => a. Id); Others solution using Linq with Where/FirstOrDefault/Any on the Id won't be more efficient than yours.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions