相关文章推荐
彷徨的机器人  ·  stringvar转变成str ...·  1 周前    · 
留胡子的汤圆  ·  SharePoint 搜索 REST ...·  2 天前    · 
没有腹肌的开水瓶  ·  Exception in thread ...·  2 天前    · 
买醉的煎饼果子  ·  Re: Where to ...·  1 年前    · 
买醉的香蕉  ·  SHA ...·  1 年前    · 
I'm reading in records from a tab-delimited flat-file, and storing those in a
C#
IEnumerable<string[]> Please note that they're are a large amount of records.
I want to use Linq to traverse through the IEnumerable records quickly, but return the whole string[] array when I find a value within one of the values of the array.
Current Situation (works fine but a little slow):
private IEnumerable<string[]> customers = GetCustomersFromCache(); foreach (string[] customer in customers) var strFound = Array.FindAll(customer, str => str.ToLower().Contains(searchText.Text.ToLower())); foreach ( string record in strFound) // doing stuff here with string array So currently I'm looping through each Customers record, and then looping through each customer record. I can then grab any part of the customer array, and use its' data.
So can Linq be used to look at the string[] within the IEnumerable, determine if any part of the array contains the string I'm searching for, and finally return the whole array?
Which means basically making the following 1 Linq command:
foreach (string[] customer in customers) var strFound = Array.FindAll(customer, str => str.ToLower().Contains(searchText.Text.ToLower())); customers.Where(c => Array.FindAll(c, str => str.ToLower().Contains(searchText.Text.ToLower())).Count() > 0 );
I don't know if it performs any better though.
[Edit]
This is probably a little faster, as it only looks for the first match, enough to tell you if anything is there:
customers.Where(c => !String.IsNullOrEmpty(Array.Find(c, str => str.ToLower().Contains(searchText.Text.ToLower()))));
You can add, for example, ".Take(25)" at the end, however that won't make the search stop when it gets to 25, it will simply give you the first 25 results of however many it finds, so it won't be faster (unless the compiler does some optimizations I'm unaware of). I'm not aware of a way to make it stop once a certain number of results is found through LINQ. If you have performance critical code you may be better off writing your own searches, I've heard it said that LINQ itself can be rather slow.
customers.Where(c => Array.FindAll(c, str => str.ToLower().Contains(searchText.Text.ToLower())).Any());
This will stop the search once the first is found.
Reason for my downvote: Previous solutions use Contains, your solution is case-sensitive whereas the others are not, your solution returns a bool whereas the OP required the string array to be returned and you have a List of type 'string' whereas the OP had a "list" (IEnumerable) of type 'string[]'.
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •