如何在 C# 中的 List<string> 中使用 Exist
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/17108943/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use Exist in List<string> in C#
提问by GenZiy
I have to find if string exist in a list to avoid duplicates inserts: Here is example from Microsoft site:
我必须查找列表中是否存在字符串以避免重复插入:以下是来自 Microsoft 站点的示例:
using System;
using System.Collections.Generic;
public class Example
{
    public static void Main()
    {
        List<string> dinosaurs = new List<string>();
        dinosaurs.Add("Compsognathus");
        dinosaurs.Add("Amargasaurus");
        dinosaurs.Add("Oviraptor");
        dinosaurs.Add("Velociraptor");
        dinosaurs.Add("Deinonychus");
        dinosaurs.Add("Dilophosaurus");
        dinosaurs.Add("Gallimimus");
        dinosaurs.Add("Triceratops");
        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }
        Console.WriteLine("\nTrueForAll(EndsWithSaurus): {0}",
            dinosaurs.TrueForAll(EndsWithSaurus));
        Console.WriteLine("\nFind(EndsWithSaurus): {0}", 
            dinosaurs.Find(EndsWithSaurus));
        Console.WriteLine("\nFindLast(EndsWithSaurus): {0}",
            dinosaurs.FindLast(EndsWithSaurus));
        Console.WriteLine("\nFindAll(EndsWithSaurus):");
        List<string> sublist = dinosaurs.FindAll(EndsWithSaurus);
        foreach(string dinosaur in sublist)
        {
            Console.WriteLine(dinosaur);
        }
        Console.WriteLine(
            "\n{0} elements removed by RemoveAll(EndsWithSaurus).", 
            dinosaurs.RemoveAll(EndsWithSaurus));
        Console.WriteLine("\nList now contains:");
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }
        Console.WriteLine("\nExists(EndsWithSaurus): {0}", 
            dinosaurs.Exists(EndsWithSaurus));
    }
    // Search predicate returns true if a string ends in "saurus".
    private static bool EndsWithSaurus(String s)
    {
        return s.ToLower().EndsWith("saurus");
    }
}
Is it possible to replace EndsWithSaurusfunction with lambda expression? 
Thanks everybody for your input!! Here is a working code:    
是否可以EndsWithSaurus用 lambda 表达式替换函数?感谢大家的投入!!这是一个工作代码:    
        if (dinosaurs.Any(e => e.EndsWith("saurus")))
            Console.WriteLine("saurus exists");
        if (dinosaurs.Exists(e => e.EndsWith("saurus")))
            Console.WriteLine("saurus exists");
采纳答案by Sevenate
Try this:
尝试这个:
if (dinosaurs.Exists(e => e.EndsWith("saurus")))
        Console.WriteLine("saurus exists");
The answer with Any()works fine too. The difference is just the Exists()method comes from List<T>itself and the Any()is just one of the great Linq extension methods (and will require using System.Linq)
的答案也Any()很好用。不同之处在于Exists()方法来自于List<T>它本身,Any()它只是一个伟大的 Linq 扩展方法(并且需要using System.Linq)
回答by D Stanley
Use Any:
使用Any:
if (dinosaurs.Any(e => e.EndsWith("saurus")))
        Console.WriteLine("saurus exists");
You coulduse List.Exists()by just changing your lambda:
您只需更改 lambda即可使用List.Exists():
if (dinosaurs.Exists(e => e.EndsWith("saurus"))   // == true is implied
        Console.WriteLine("saurus exists");
but Anyis more portable (i.e. can be used with anyenumerable, not just Lists.
但Any更便携(即可以与任何可枚举一起使用,而不仅仅是Lists.

