SolutionCottage.com
Posted  12 Mar 2009 - 22:31:14

Category: ASP.net


Search in a generic ASP.net List

Issue:
You want to find a specific object in a generic List using ASP.net (C#).

Solution:
Imagine a list with some objects, and you wish to search for an object with a specific Id. Here is an example code file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ListSearch
{
class Program
{
static void Main(string[] args)
{
List<Category> categories = new List<Category>();
categories.Add(new Category("1", "PDFs"));
categories.Add(new Category("2", "DOCs"));
categories.Add(new Category("3", "TXTs"));
categories.Add(new Category("4", "RTFs"));
Category locatedCategory = categories.Find(delegate(Category p) { return p.Id == "1"; });

Console.WriteLine(locatedCategory.Name);
Console.ReadKey();
}
}

class Category
{
public string Id;
public string Name;


public Category(string Id, string Name)
{
this.Id = Id;
this.Name = Name;
}
}
}








0 comments:








© SolutionCottage 2008-2011 7/21/2017 3:37:25 AM