Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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):
C#
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:
C#
foreach (string[] customer in customers)
{
    var strFound = Array.FindAll(customer, str => str.ToLower().Contains(searchText.Text.ToLower()));
}
Posted
Updated 28-Oct-21 2:56am
Comments
Wonde Tadesse 27-Jun-12 18:20pm    
"string[] customer" you mean to say var customer. o_O

Would this do what you want?

C#
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:
C#
customers.Where(c => !String.IsNullOrEmpty(Array.Find(c, str => str.ToLower().Contains(searchText.Text.ToLower()))));
 
Share this answer
 
v2
Comments
kbiz2 27-Jun-12 16:44pm    
This worked. Thanks very much! Now I'm working on the performance piece of it. Any thoughts?
kbiz2 27-Jun-12 16:49pm    
Actually for a really large search within the array it only took 2 seconds. So that part is working well!
lewax00 27-Jun-12 16:58pm    
I added another one that might be a little faster. I don't know too much about optimizing LINQ, I've never really had a reason to (yet).
kbiz2 27-Jun-12 17:20pm    
I will give it a try. Thx!! In addition, there must be a way to limit the search as well, meaning can I specify how many records I want to return from the search? 25 or 50, or 100?
lewax00 27-Jun-12 17:33pm    
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.
This would be better than solution 1:
C#
customers.Where(c => Array.FindAll(c, str => str.ToLower().Contains(searchText.Text.ToLower())).Any());


This will stop the search once the first is found.
 
Share this answer
 
v3
Comments
kbiz2 27-Jun-12 17:01pm    
This worked well as well! Thanks!
Clifford Nelson 27-Jun-12 17:17pm    
Did it give you much performance improvement? Also, mark question as answered. The first guy had answered the question.
kbiz2 27-Jun-12 17:21pm    
Your search came in at 2 seconds as well. The search itself is looking at 1.4 million string[]'s, so I thought 2 seconds was pretty impressive.
A smaller and easier to understand solution, just go to a List and check with "Contains".

List<string> lstCustomers = GetCustomersFromCache().ToList();

bool test = lstCustomers.Contains("yourText");
 
Share this answer
 
Comments
CHill60 28-Oct-21 9:17am    
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[]'.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900