Find on a List<> of objects

Tom Knowlton
Tom Knowlton used Ask the Experts™
on
public class StateData
    {
        public StateData(string tempabbrev, string tempfullstatename)
        {
            abbrev = tempabbrev;
            fullstatename = tempfullstatename;
        }

        public string abbrev = "";
        public string fullstatename = "";
    }


I have a collection of states in another list .... if any of those are found in my List<StateData> I want to do something.


Please provide sample C# code.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
List<string> states = new List<string>();
List<StateData> data = new List<StateData>();
//fill the values with what you want into those lists
            int count = (from x in data
                       where (from y in states where y == x.fullstatename select y).Count() > 0
                       select x).Count();
if(count > 0)
{
    //do something
}
Tom KnowltonWeb developer

Author

Commented:
My eventual solution:


 foreach (string s in tempstates)
            {
                foreach (FinishPointLeadCollator.StateData sc in this._fmlc._statedata)
                {
                    if (s == sc.abbrev)
                    {
                        sc.itemischecked = true;
                    }
                }
            }

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial