0

I have a list containing the following structure.

class CompareDesignGroup
{
    string FieldId;
    string Caption;
}

The list is containing items of the above structure.

Is it possible to retrieve an element of the list if FieldId is known?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
NIlesh Lanke
  • 1,213
  • 9
  • 26
  • 35

6 Answers6

6

You can use the Find method on the generic list class. The find method takes a predicate that lets you filter/search the list for a single item.

List<CompareDesignGroup> list = // ..;
CompareDesignGroup item = list.Find(c => c.FieldId == "SomeFieldId");

item will be null if there is no matching item in the list.

If you need to find more than one item you can use the FindAll method:

List<CompareDesignGroup> list = // ..;
List<CompareDesignGroup> result= list.FindAll(c => c.FieldId == "SomeFieldId");
Stefan
  • 14,530
  • 4
  • 55
  • 62
2

You can use Where and then you can use FirstOrDefault. That is an LINQ expression.

var ls = new List<CompareDesignGroup>();
var result = ls.Where(a => a.FieldId=="123").FirstOrDefault();

Or SingleOrDefault to get the item you want. Like this:

var ls = new List<CompareDesignGroup>();
var result = ls.Where(a => a.FieldId=="123").SingleOrDefault()

Or even simpler:

var result = ls.SingleOrDefault(a => a.FieldId=="123");
var result2 = ls.FirstOrDefault(a => a.FieldId=="123");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arion
  • 31,011
  • 10
  • 70
  • 88
2

Yes. Use LINQ or the built-in functionalities of List.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
iefpw
  • 6,816
  • 15
  • 55
  • 79
2

You can use LINQ like this:

CompareDesignGroup result = yourList.FirstOrDefault(x => x.FieldId == yourKnownId);

If you use the FirstOrDefault method the result will be null when list doesn't contain a record with a known id. So before using result check if it is not null.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Renatas M.
  • 11,694
  • 1
  • 43
  • 62
2

There are a plethora of methods to find an item inside a list.

LINQ provides extensions method useful to work with collections that does not provide their own search features (or when you do not have the collection itself but a generic interface like IEnumerable<T>). If you have a List<CompareDesignGroup> object and you'll work on that object you can use the methods provided by that class (specialized methods are almost always faster than LINQ methods, they know collection's internal structure and does not have to rely on many abstraction layers).

In all examples I'll perform a culture invariant and case sensitive comparison for FieldId to a hypothetical id parameter. This may not be what you need and you may have to change according to your requirements.

Using List<T>

Given a list declared as:

List<CompareDesignGroup>() list = new List<CompareDesignGroup>();

To find first element that matches the search criteria (it'll return null if no items have been found):

CompareDesignGroup item = list.Find(
    x => String.Equals(x.FieldId, id,  StringComparison.InvariantCulture));

To find all the elements that matches the search criteria:

List<CompareDesignGroup> items = list.FindAll(
    x => String.Equals(x.FieldId, id,  StringComparison.InvariantCulture));

Using IEnumerable<T> (or IList<T>, for example)

Given a list declared as:

IEnumerable<CompareDesignGroup> list = ...

To find first element that matches the search criteria (null if no items have been found):

CompareDesignGroup item = list.FirstOrDefault(
    x => String.Equals(x.FieldId, id,  StringComparison.InvariantCulture));

To find the first element that matches the search criteria (or throw an exception if no items have been found):

CompareDesignGroup item = list.First(
    x => String.Equals(x.FieldId, id,  StringComparison.InvariantCulture));

To find all elements that matches the search criteria:

IEnumerable<CompareDesignGroup> item = list.Where(
    x => String.Equals(x.FieldId, id,  StringComparison.InvariantCulture));

There are many LINQ extensions methods, I suggest to take a look to them all to find the one that better suits your needs.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
1
List<CompareDesignGroup> listData = new List<CompareDesignGroup>(); // init the data
var result = listData.Where(x=> String.Equals(x.FieldID,"FIELDID KNOWN VALUE"); // gets all data
var first = listData.FirstOrDefault(x=> String.Equals(x.FieldID,"FIELDID KNOWN VALUE"); // gets first search result
Tilak
  • 30,108
  • 19
  • 83
  • 131