ChatGPT解决这个技术问题 Extra ChatGPT

Find an item in a list by LINQ

Here I have a simple example to find an item in a list of strings. Normally I use a for loop or anonymous delegate to do it like this:

int GetItemIndex(string search)
{
   int found = -1;
   if ( _list != null )
   {
     foreach (string item in _list) // _list is an instance of List<string>
     {
        found++;
        if ( string.Equals(search, item) )
        {
           break;
        }
      }
      /* Use an anonymous delegate
      string foundItem = _list.Find( delegate(string item) {
         found++;
         return string.Equals(search, item);
      });
      */
   }
   return found;
}

LINQ is new for me. Can I use LINQ to find an item in the list? If it is possible, how?

That's great. However, those are all lamda expression style. I use a simple list here. The list may be a class with several properties and some are used for search. So any LINQ way to search like "from .. in ... where... select..."
Nah, sorry. Most of these methods (First, Single, Any, ...) cannot be directly translated to that form.
Nevermind, actually you can get rid of the lambdas for a few cases...
Great answers! I just want to get a taste of LINQ searching from enumeration case.

P
Peter Mortensen

There are a few ways (note that this is not a complete list).

Single will return a single result, but will throw an exception if it finds none or more than one (which may or may not be what you want): string search = "lookforme"; List myList = new List(); string result = myList.Single(s => s == search);

Note that SingleOrDefault() will behave the same, except it will return null for reference types, or the default value for value types, instead of throwing an exception.

Where will return all items which match your criteria, so you may get an IEnumerable with one element: IEnumerable results = myList.Where(s => s == search); First will return the first item which matches your criteria: string result = myList.First(s => s == search);

Note that FirstOrDefault() will behave the same, except it will return null for reference types, or the default value for value types, instead of throwing an exception.


Great answer. I found SingleOrDefault to be my answer of choice - same as Single but returns 'null' if it can't find it.
I didn't know either Single() or SingleOrDefault(). Very useful.
Can these methods be used with other collections too like ReadOnlyCollection or ObservableCollection?
@yellavon these are extension methods on any type that implements IEnumerable<T> or IQueryable<T>
One thing to note about using SingleOrDefault is that because it throws an exception if more than one match is in the list, it has to iterate through every item, where FirstOrDefault will stop searching once the first match is found. msdn.microsoft.com/en-us/library/bb342451(v=vs.110).aspx
9
9 revs, 2 users 98%

If you want the index of the element, this will do it:

int index = list.Select((item, i) => new { Item = item, Index = i })
                .First(x => x.Item == search).Index;

// or
var tagged = list.Select((item, i) => new { Item = item, Index = i });
int index = (from pair in tagged
            where pair.Item == search
            select pair.Index).First();

You can't get rid of the lambda in the first pass.

Note that this will throw if the item doesn't exist. This solves the problem by resorting to nullable ints:

var tagged = list.Select((item, i) => new { Item = item, Index = (int?)i });
int? index = (from pair in tagged
            where pair.Item == search
            select pair.Index).FirstOrDefault();

If you want the item:

// Throws if not found
var item = list.First(item => item == search);
// or
var item = (from item in list
            where item == search
            select item).First();

// Null if not found
var item = list.FirstOrDefault(item => item == search);
// or
var item = (from item in list
            where item == search
            select item).FirstOrDefault();

If you want to count the number of items that match:

int count = list.Count(item => item == search);
// or
int count = (from item in list
            where item == search
            select item).Count();

If you want all the items that match:

var items = list.Where(item => item == search);
// or
var items = from item in list
            where item == search
            select item;

And don't forget to check the list for null in any of these cases.

Or use (list ?? Enumerable.Empty<string>()) instead of list.


Two points. First, there's no real need to use string.Equals here - nothing wrong with ==. Second, I'd also mention FirstOrDefault (for cases where item might not be there), and Select with index to cover the case where index of item is needed (as it is in the sample in the question itself).
I'm not happy yet. There's no -1 index (not found) in my example. Any suggestion?
Besides checking for it's existance with if, first.
Do I need to check if list is null first?
Select throws ArgumentNullException if the source is null
A
AgileJon

If it really is a List<string> you don't need LINQ, just use:

int GetItemIndex(string search)
{
    return _list == null ? -1 : _list.IndexOf(search);
}

If you are looking for the item itself, try:

string GetItem(string search)
{
    return _list == null ? null : _list.FirstOrDefault(s => s.Equals(search));
}

Following the logic of the first example, we could use _list.Find(search) for the second.
K
Kelsey

Do you want the item in the list or the actual item itself (would assume the item itself).

Here are a bunch of options for you:

string result = _list.First(s => s == search);

string result = (from s in _list
                 where s == search
                 select s).Single();

string result = _list.Find(search);

int result = _list.IndexOf(search);

how about index as a return value?
and do I need to check if _list is null in the form of from .. in _list...?
R
RckLN

This method is easier and safer

var lOrders = new List<string>();

bool insertOrderNew = lOrders.Find(r => r == "1234") == null ? true : false


I think we don't even need true : false below should work same bool insertOrderNew = lOrders.Find(r => r == "1234") == null;
C
Colonel Panic

How about IndexOf?

Searches for the specified object and returns the index of the first occurrence within the list

For example

> var boys = new List<string>{"Harry", "Ron", "Neville"};  
> boys.IndexOf("Neville")  
2
> boys[2] == "Neville"
True

Note that it returns -1 if the value doesn't occur in the list

> boys.IndexOf("Hermione")  
-1

P
Peter Mortensen

This will help you in getting the first or default value in your LINQ List search

var results = _List.Where(item => item == search).FirstOrDefault();

This search will find the first or default value, which it will return.


W
Will Marcouiller

I used to use a Dictionary which is some sort of an indexed list which will give me exactly what I want when I want it.

Dictionary<string, int> margins = new Dictionary<string, int>();
margins.Add("left", 10);
margins.Add("right", 10);
margins.Add("top", 20);
margins.Add("bottom", 30);

Whenever I wish to access my margins values, for instance, I address my dictionary:

int xStartPos = margins["left"];
int xLimitPos = margins["right"];
int yStartPos = margins["top"];
int yLimitPos = margins["bottom"];

So, depending on what you're doing, a dictionary can be useful.


Great answer to a different question.
S
Stephen Byrne

If we need to find an element from the list, then we can use the Find and FindAll extensions method, but there is a slight difference between them. Here is an example.

 List<int> items = new List<int>() { 10, 9, 8, 4, 8, 7, 8 };

  // It will return only one 8 as Find returns only the first occurrence of matched elements.
     var result = items.Find(ls => ls == 8);      
 // this will returns three {8,8,8} as FindAll returns all the matched elements.
      var result1 = items.FindAll(ls => ls == 8); 

R
René Vogt

Here is one way to rewrite your method to use LINQ:

public static int GetItemIndex(string search)
{
    List<string> _list = new List<string>() { "one", "two", "three" };

    var result = _list.Select((Value, Index) => new { Value, Index })
            .SingleOrDefault(l => l.Value == search);

    return result == null ? -1 : result.Index;
}

Thus, calling it with

GetItemIndex("two") will return 1,

and

GetItemIndex("notthere") will return -1.

Reference: linqsamples.com


P
Peter Mortensen

Try this code:

return context.EntitytableName.AsEnumerable().Find(p => p.LoginID.Equals(loginID) && p.Password.Equals(password)).Select(p => new ModelTableName{ FirstName = p.FirstName, UserID = p.UserID });

Perhaps break it into several lines?
H
Huseyin Durmus

You want to search an object in object list.

This will help you in getting the first or default value in your Linq List search.

var item = list.FirstOrDefault(items =>  items.Reference == ent.BackToBackExternalReferenceId);

or

var item = (from items in list
    where items.Reference == ent.BackToBackExternalReferenceId
    select items).FirstOrDefault();

Part of this is plagiarised from befree2j's answer (see the previous revision).
P
Peter Mortensen

You can use FirstOfDefault with the Where LINQ extension to get a MessageAction class from the IEnumerable. Reme

var action = Message.Actions.Where(e => e.targetByName == className).FirstOrDefault<MessageAction>();

where

List<MessageAction> Actions { get; set; }

P
Peter Mortensen

One more way to check the existence of an element in a List:

var result = myList.Exists(users => users.Equals("Vijai"))