I'm reading in records from a tab-delimited flat-file, and storing those in a
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)
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()))));
customers.Where(c => Array.FindAll(c, str => str.ToLower().Contains(searchText.Text.ToLower())).Any());
This will stop the search once the first is found.
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.