How to search collection

  • Thread starter Thread starter abcd
  • Start date Start date
A

abcd

I have a generic list of objects. Each object contains multiple objects and
one of the object has some properties. Now I need to filter the list based on
those properties. Whats the best way for me. I tried foreach loop but I am
unable to write the filter criteria. All the proeperties I need to filter
must have AND operation. I need to consider conditions like blank etc. To use
list.Find I need to write predicate...any samples anywhere...

thanks
 
I have a generic list of objects. Each object contains multiple objects
and
one of the object has some properties. Now I need to filter the list
based on
those properties. Whats the best way for me. I tried foreach loop but I
am
unable to write the filter criteria. All the proeperties I need to filter
must have AND operation. I need to consider conditions like blank etc.
To use
list.Find I need to write predicate...any samples anywhere...

http://msdn2.microsoft.com/en-us/library/x0b5b5bc(VS.85).aspx

Was there something about the sample code on MSDN that you're having
trouble with? The code sample for List.Find() demonstrates the use of a
predicate.

It will be difficult to answer your question without something more
specific to go on. For example, what have you tried already? Why did it
not work? What about the existing sample code isn't clear?

Pete
 
///////////////////////////////////////////////////////
using System;
using System.Drawing;

public class Example
{
public static void Main()
{
// Create an array of five Point structures.
Point[] points = { new Point(100, 200),
new Point(150, 250), new Point(250, 375),
new Point(275, 395), new Point(295, 450) };

// To find the first Point structure for which X times Y
// is greater than 100000, pass the array and a delegate
// that represents the ProductGT10 method to the Shared
// Find method of the Array class.
Point first = Array.Find(points, ProductGT10);

// Note that you do not need to create the delegate
// explicitly, or to specify the type parameter of the
// generic method, because the C# compiler has enough
// context to determine that information for you.

// Display the first structure found.
Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y);
}

// This method implements the test condition for the Find
// method.
private static bool ProductGT10(Point p)
{
if (p.X * p.Y > 100000)
{
return true;
}
else
{
return false;
}
}
}

/* This code example produces the following output:

Found: X = 275, Y = 395
*/
 
You don't indicate what toolset you are using... if you are using .NET
3.5 and VS 2008, then LINQ provides an interesting way to build
complex composable queries - consider the following; but perhaps the
most interesting bit is that this same code will work against in-
memory collections and a SQL-Server database (or indeed, /any/
suitable provider), with the latter building the appropriate "WHERE"
SQL for you (i.e. it doesn't get everything and then filter locally).

class SomeType
{
public string Name { get; set; }
public int Value { get; set; }
public bool IsActive { get; set; }
}
class Program
{
static void Main()
{
List<SomeType> data = new List<SomeType>();
//TODO: fill the list...

// check the search options...
// presumably from a search UI
// or similar
string searchName = "whatever";
int? searchValue = null; // (no value entered...)
bool? searchIsActive = true;

// prepare a query (deferred execution)
IEnumerable<SomeType> results = data;
if (!string.IsNullOrEmpty(searchName)) {
// if a name is provided, ensure it matches
results = data.Where(item => item.Name == searchName);
}
if (searchValue != null) {
// if a value is provided, ensure it matches
results = data.Where(item => item.Value ==
searchValue.Value);
}
if (searchIsActive != null) {
// if the flag is provided, ensure it matches
results = data.Where(item => item.IsActive ==
searchIsActive.Value);
}
// show the results... (i.e. all those that matched)
foreach (var item in results) {
Console.WriteLine("{0}, {1}: {2}", item.Name,
item.Value, item.IsActive);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top