tags:

views:

113

answers:

4

I have a list of my custom class(which has properties like Name,Age,Address).How can i check whether i have an item with the "Name" value as "shyju" exist in the list and return it if it exist.Name will be unique.No two items have the same name.

the solution now i am thinking of is to go for a for each loop and loop thru each item and check the Name of each item with "shyju" and return it.

Is there any other way to do it ?

+11  A: 

The following will return the item with the provided name if there is only one instance in the list (it will throw if there is more than one element).

var item = list.SingleOrDefault(x => x.Name=="shyju");
if ( item != null ) { 
  ...
}

It does do a bit of extra work to guarantee there is only one item in the list with this name. If you already have established that then you can speed it up a bit by using FirstOrDefault instead

var item = list.FirstOrDefault(x => x.Name=="shyju");
if ( item != null ) { 
  ...
}
JaredPar
It should be mentioned that the first variant will *throw an exception* if more than two entries match the criterion.
chiccodoro
@chiccodoro, I keep forgetting about that annoying side effect. Will update.
JaredPar
How do we handle if there is more than item with same value. Just out of my curiousity
Shyju
@Shyju: I don't understand your question, that's exactly what JaredPar has written and our talk has been about: SingleOrDefault will throw an exception, FirstOrDefault will ignore the fact. Take whichever you need for your case.
chiccodoro
@JaredPar: BTW: What's the difference for Find(x => x.Name="shyju")?
chiccodoro
@JaredPar: Oh, I think I know it: Find is implemented by List, while the others are extension methods by LINQ.
chiccodoro
@Shyju then you likely want to use .Where to get the collection of all objects matching the predicate.
JaredPar
@Shyju `Find` is different in that it's an instance method on `List<T>` while `FirstOrDefault` is an extension method on `IEnumerable<T>` and hence usable on more types. They are functionally equivalent methods though.
JaredPar
@Jared please modify your code to x.Name == "shyju" instead of x.Name = "shyju"
sebastian
@sebastian, gotta love the early morning typos (fixed)
JaredPar
+2  A: 

You can use HashSet<T> instead of List<T>.
And implement Equals and GetHashCode in your custom class.

Itay
+2  A: 
Contact contact = contacts.FirstOrDefault(o => o.Name == "shyju");
cement
+3  A: 

I have a list of my custom class(which has properties like Name,Age,Address).How can i check whether i have an item with the "Name" value as "shyju" exist in the list and return it if it exist.Name will be unique.No two items have the same name.

If the list is going to be unique, how about implmenting, not as a list but as a dictionary?

Dictionary<string, MyCustomClass> myCustomDictionary;
// code to populate the dictionary goes here...

Then you would only need to do this:

var person = myCustomDictionary["shyju"];
code4life
Upvote for reading my mind. :)
mikemanne