Internal join on 2 arrays?

I am trying to find a solution to the following (I think with Linq):

I need to pull certain files from a larger list of files on an ftp server with a similar file name. For example, we send an order file to a company for processing, then return an answer file that we can download.

So, I can send them the files "order_123.txt" and "order_456.txt". After some time, I need to search and download answers for those files that will be called "order_123.resp" and "order_456.resp". A kicker is that in some cases I can have several answers, in which case they would create "order_123-1.resp" and "order_123-2.resp", and also the files would not be deleted from the server.

I know this can be achieved by scrolling through the files that I know I need answers to then skip all the files on the server until I find the files that match, but I hope I don't need to go through the files on the server more than once.

This example may help clarify:

I 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 upload 3rd, 4th and 5th files.

Thank.

+3
source share
2 answers

Here is one way to do this:

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);

Conclusion:

order_222-1.resp
order_222-2.resp
order_333.resp

. , (Dictionary, HashSet ..).

0

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;
0

Source: https://habr.com/ru/post/1737429/


All Articles