Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

LINQ Query: Multiple "StartsWith"

Posted on 2011-03-15
5
Medium Priority
?
1,900 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

715 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