简体   繁体   中英

List.Any get matched String

FilePrefixList.Any(s => FileName.StartsWith(s))

Can I get s value here? I want to display the matched string.

Any determines only if there is a match, it doesn't return anything apart from the bool and it needs to execute the query.

You can use Where or First / FirstOrDefault :

string firstMastch = FilePrefixList.FirstOrDefault(s => FileName.StartsWith(s)); // null if no match

var allMatches = FilePrefixList.Where(s => FileName.StartsWith(s));
string firstMastch = allMatches.FirstOrDefault(); // null if no match

So Any is fine if all you need to know is if ther's a match, otherwise you can use FirstOrDefault to get the first match or null (in case of reference types).

Since Any needs to execute the query this is less efficient:

string firstMatch = null;
if(FilePrefixList.Any(s => FileName.StartsWith(s)))
{
    // second execution
    firstMatch = FilePrefixList.First(s => FileName.StartsWith(s));
}

If you want to put all matches into a separate collection like a List<string> :

List<string> matchList = allMatches.ToList(); // or ToArray()

If you want to output all matches you can use String.Join :

string matchingFiles = String.Join(",", allMatches);  

Not with Any , no... that's only meant to determine whether there are any matches, which is why it returns bool . However, you can use FirstOrDefault with a predicate instead:

var match = FilePrefixList.FirstOrDefault(s => FileName.StartsWith(s));
if (match != null)
{
    // Display the match
}
else
{
    // Nothing matched
}

If you want to find all the matches, use Where instead.

if FilePrefixList is a List<string> , you can use List<T>.Find method:

string first = FilePrefixList.Find(s => FileName.StartsWith(s));

fiddle: List.Find vs LINQ (Find is faster)

List<T>.Find (MSDN) returns the first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T

Enumerable.Any() returns bool denoting whether any item matched the criteria.

If you need the matched item, use SingleOrDefault() instead:

var matchedPrefix = FilePrefixList.SingleOrDefault(s => FileName.StartsWith(s));

See MSDN

please check try this: we assuming FilePrefixList is collectionlist

 class A
        {
            public int ID { get; set; }
            public string Name { get; set; }
        }

 List<A> FilePrefixList= new  List<A>();
             FilePrefixList.Add(new A
            {
                ID = 1,
                Name = "One"
            });
            FilePrefixList.Add(new A
            {
                ID =2,
                Name = "Two"
            });
            FilePrefixList.Add(new A
            {
                ID = 3,
                Name = "Three"
            });

select data from list is:

var listItems = FilePrefixList.Where(x =>x.Name.StartsWith("T")).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM