Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9011545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:45:49+00:00 2026-06-16T02:45:49+00:00

A seemingly simple problem: I have a list of strings (read from a property

  • 0

A seemingly simple problem:
I have a list of strings (read from a property file):

IList<string> defaultValues = new List<string> {"0458","0309"};

and another list of some Objects, each having a property of type string:

IList<Token> tokens = new List<Token>
{
    new Token {DisplayValue = "0123"},
    new Token {DisplayValue = "0309"},
    new Token {DisplayValue = "0203"},
    new Token {DisplayValue = "0458"},
    new Token {DisplayValue = "0911"}
};

public class Token
{
   public string DisplayValue { get; set; }
}

Now I would like to get the element of tokens where DisplayValue matches the first element of defaultValues (0485). If no element with DisplayValue 0485 is found, the second element in defaultvalues should match (0309) and so on.

The defaultValues List can be dynamic, so more values could be added, and always the first entry should have priority.

So the defaultValues list is sort of a priority list of strings, the lower the index the higher the priority.
In the example above, the result should be “0458”.

I could do something like this:

string result = string.Empty;

foreach (var searchValue in defaultValues)
{
    if (tokens.Any(token => token.DisplayValue == searchValue))
    {
        result = searchValue;
    }   
}

But I think something like this could be done more elegantly and with no foreach …

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:45:51+00:00Added an answer on June 16, 2026 at 2:45 am

    Using Any is a O(n^2) in this case, which is probably not the most performant. Probably the best way to do this would be to do a GroupJoin (making it O(n) instead) and then select the first token that was matched. Just keep in mind that GroupJoin only does it on exactly matching keys, so if you are looking for a comparison or substring search, you’ll have to find other ways.

    defaultValues
        .GroupJoin(
            tokens, // matching 0:n tokens per default value
            defVals => defVals, // key selector for our left source
            tks => tks.DisplayValue, // key selector for our right source
            (defVal, tks) => tks.FirstOrDefault()) // result selector for our matches
        .FirstOrDefault(match => match != null)
    

    This has the advantage of only traversing each collection once, and matches all tokens with the corresponding DefaultValue (so you could have multiple matches per default value). You COULD do defaultValues.FirstOrDefault(x => tokens.Any(t => t == x)), but you’d run into the same problem (with possible O(n^2) complexity). From here, you can just check whether it’s null (use null coalescing) and add a static constant called Empty in Token that initializes to new Token { DisplayValue = string.Empty }. After that you can do this:

    (defaultValues
        .GroupJoin(
            ...
        .FirstOrDefault(match => match != null) ?? Token.Empty).DisplayValue
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a (seemingly) simple question to read in a string and print it
I have a seemingly simple problem of splitting a comma separated String into tokens,
I have a seemingly simple problem whereby I wish to reconcile two lists so
I have a seemingly simple problem, but I can't quite figure out a solution.
I have a seemingly simple problem, but an easy solution is alluding me. I
I'm stumped by a seemingly simple problem. In my ASP.NET page, I have a
Good day once again. I have a (seemingly) simple problem. I'm trying to display
Howdy, codeboys and codegirls! I have came across a simple problem with seemingly easy
I have been trying to get this seemingly simple problem fixed for about half
I have a puzzling problem: this seemingly simple page doesn't work in IE9: <?xml

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.