Inner join on 2 arrays?
I'm trying to find a solution to the following (I'm thinking with Linq) to the following:
I need to pull down certain files from a larger list of files on an ftp server that have a similar file name. For example, we send an order file to some company for processing then they return a response file that we can download.
So, I could send them the files "order_123.txt" and "order_456.txt". After a certain amount of time I need to go look for and download the responses for those files that will be named "order_123.resp" and "order_456.resp". The kicker is that in some cases I can have multiple responses in which case they would create "order_123-1.resp" and "order_123-2.resp" and also the files don't get removed from the server.
I know this can be accomplished by looping through the files I know I need responses to then loop thro开发者_如何学编程ugh all the files on the server until I find files that match but I'm hoping that I don't have to loop through the files on the server more than once.
This example may help clarify:
I've sent "order_222.txt" and "order_333.txt" they processed them and the ftp server contains: "order_111-1.resp" "order_001.resp" "order_222-1.resp" "order_222-2.resp" "order_333.resp"
I need to download the 3rd, 4th, and 5th file.
Thanks.
Here's one way to do it:
string[] requests = { "order_222.txt", "order_333.txt" };
string[] responses = {
"order_111-1.resp",
"order_001.resp",
"order_222-1.resp",
"order_222-2.resp",
"order_333.resp"
};
Regex regex = new Regex(@"(-\d+)?\.resp$");
IEnumerable<string> toDownload =
responses.Where(x => requests.Contains(regex.Replace(x, ".txt")));
foreach (string filename in toDownload)
Console.WriteLine(filename);
Output:
order_222-1.resp
order_222-2.resp
order_333.resp
It uses a linear lookup in the requests array each time. This could be improved by using a hashed lookup (Dictionary, HashSet, etc).
Try this
string[] requests = {
"order_222.txt",
"order_333.txt" };
string[] responses = {
"order_222.txt",
"order_333.txt",
"order_111-1.resp",
"order_001.resp",
"order_222-1.resp",
"order_222-2.resp",
"order_333.resp"
};
var r = from req in requests
from res in responses
where res.StartsWith(Path.GetFileNameWithoutExtension(req))
&& Path.GetExtension(res) == ".resp"
select res;
精彩评论