TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Search item(s) in a generic list
Anh Duc Thai
Feb 01, 2007
10.5
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
A better way to find item(s) in generic list using Predecate
Most often, we do with List
and find one or more items in list. Imagine that we want to look for a product in List
using PrimaryKey. The code is: foreach (Product p in this.Products) { if (p.PrimaryKey == primaryKey) return p; } We also have another way that makes it better in performance as well as readability. Predicate
condition = delegate(Product p) { return p.PrimaryKey.Equals(primaryKey); }; return this.Products.Find(condition); To find all items: this.Products.FindAll(condition); Both of them return NULL if not found.
Next Recommended Reading
Kumo.com - New Search by Microsoft and Yahoo