如何使用asp.net core 2.2与bson数组形成逗号分隔的字符串?

问题描述 投票:0回答:1

我有一个基于asp.net core 2.2的api,其中我正在像这样建立一个ips(字符串类型)的数组

 [HttpGet ("{nsp}/geolocation")]
    [ResponseCache (Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public async Task<dynamic> getLocation (string nsp) {
        nsp = "/"+nsp;
        // string ipInfoBaseUrl = "http://ip-api.com/json/";
        string baseUrl = "http://ip-api.com/batch";
        // string userIpAddress = "197.157.194.90";
        // string ipUrl = ipInfoBaseUrl + userIpAddress;

         // var client = _httpClientFactory.CreateClient();
        // var result = await client.PostAsync(baseUrl,new StringContent(JsonConvert.SerializeObject(finals), System.Text.Encoding.UTF8, "application/json"));
        // // var final = Newtonsoft.Json.JsonConvert.DeserializeObject<UserLocation>(result);
        // Console.WriteLine(finals+" --result-----");
        // return Ok(result);

        var match = new BsonDocument ();
        var group = new BsonDocument ();
        var project = new BsonDocument ();
        var sort = new BsonDocument ();
        var addFields = new BsonDocument ();

        var pipeline = new [] { new BsonDocument () };

         /* @Aggregation : Stage-1 */
        match = new BsonDocument ("$match",
            new BsonDocument {
                {
                    "nsp" , nsp
                }
            });

        /* @Aggregation : Stage-2 */
        group = new BsonDocument("$group", 
            new BsonDocument
                { {
                    "_id", "null"
                },
                { "geoLocations", 
                    new BsonDocument("$addToSet", "$visitor.ip") 
                }
        });

        /* @Aggregation : Stage-3 */
        project = new BsonDocument ("$project", new BsonDocument { { "_id", 0 }});


        pipeline = new [] { match, group,project};
        var list = await DbService.tickets.AggregateAsync<BsonDocument> (pipeline, new AggregateOptions { UseCursor = true, BatchSize = batchCount });
        while (await list.MoveNextAsync ()) {
            var list_real = new List<BsonValue> ();
            foreach (var data in list.Current.ToArray ()) {
                list_real.Add (data);
            }
            return list_real.ToJson ();
        }
        return new BsonArray ().ToJson ();
    }

这样返回结果

  [
    {
    "  geoLocations": [
        "122.8.208.9",
        "196.62.107.243",
        "182.188.38.219",
        "39.50.244.198",
        "39.51.40.251",
        "103.20.134.56",
        "103.228.156.83",
        "202.143.125.21",
        "196.62.151.47",
        "45.116.232.50",
        "39.57.128.75",
        "103.18.8.60",
        "202.143.125.20",
        "182.190.252.96",
        "119.153.56.2",
        "46.101.89.227",
        "196.194.172.211",
        "192.168.20.186",
        "64.233.173.146",
        "104.236.195.147",
        "39.50.156.242",
        "103.255.5.58"
       ]
      }
    ]

如何从该结果中获取逗号分隔的字符串,例如

"111.92.158.82","202.142.168.162","122.8.157.172",.....

从一开始,我就从集合中的所有文档中获取所有ip,并形成一个ips数组。但是我的最终目标是从该数组中形成一个逗号分隔的字符串,因为我必须将该逗号分隔的ips字符串传递给一个api获取ips位置。

我正在使用asp.net corec#。我该如何实现?

c# api asp.net-core json.net
1个回答
0
投票

IEnumerable<string>替换方法签名,并用:替换方法:

var list = await DbService.tickets.AggregateAsync<BsonDocument> (pipeline, 
           new AggregateOptions 
           {
             UseCursor = true, 
             BatchSize = batchCount 
           });
var result = new List<string>();       
while (await list.MoveNextAsync())             
    result.AddRange(list.Current.Cast<string>());

return string.Join(',', result);
© www.soinside.com 2019 - 2024. All rights reserved.