Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

LINQ Query: Multiple "StartsWith"

Posted on 2011-03-15
5
Medium Priority
?
1,963 Views
Last Modified: 2012-05-11
Briefly:
      string[] myarray = { "banana", "apple", "badminton","sansa stark" };
      string[] lookup = {"a", "ban"};
      string[] query = ???;

How do I use "lookup" as a "StartsWith" filter?  Query should return "apple" and "banana".  Both method-based and the verbose version would be greatly appreciated...
0
Comment
Question by:tknudsen-qec
  • 2
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 35138647
Below you can find an example with a list of strings (which is in my eyes a better solution)
public static class StringExtensions 
    { 
        public static bool StartsWithAny(this string s, IEnumerable<string> items) 
        { return items.Any(i => s.StartsWith(i)); 
        } 
    } 

   public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            List<string> myarray = new List<string>() { "banana", "apple", "badminton", "sansa stark" };
            List<string> lookup = new List<string>{"a", "ban"};
            var query = from x in myarray where x.StartsWithAny(lookup) select x;
            foreach (string x in query)
            {
                MessageBox.Show (x);
            }
        }

}

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 35138660
Added a small example how you can easily transform your string[] into list<string> and use the same query
public static class StringExtensions 
    { 
        public static bool StartsWithAny(this string s, IEnumerable<string> items) 
        { return items.Any(i => s.StartsWith(i)); 
        } 
    } 


        private void Form1_Load(object sender, EventArgs e)
        {

            string[] myList = { "banana", "apple", "badminton","sansa stark" };
            string[] lookupList = {"a", "ban"};


            List<string> StringtoList = new List<string>(myList.Length);//AddRange is a method of List<T> objects that enables the conversion.//We just need to pass the reference to the array.            
            StringtoList.AddRange(myList);

            List<string> LookuptoList = new List<string>(lookupList.Length);//AddRange is a method of List<T> objects that enables the conversion.//We just need to pass the reference to the array.            
            LookuptoList.AddRange(lookupList);

            query = from x in myarray where x.StartsWithAny(lookup) select x;
}

}

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 35138832
Here's my offering:
string[] queryResult = myarray.Where(item => lookup.Any(l => item.StartsWith(l))).ToArray();

Open in new window

0
 
LVL 3

Author Closing Comment

by:tknudsen-qec
ID: 35138967
kaufmed: works great and is clean.  Thanks kindly.

I couldn't get prior solution to work although at a glance I think it just needed a bit of cleaning up and a refresher on method extensions.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35139211
NP. Glad to help  : )
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question