首页 > 解决方案 > 如何将区域列表与“地址”(字符串)匹配并获取列表和字符串中都存在的区域 Asp.net core

问题描述

大家好,我有包含来自 Db 的地址区域列表的字符串,我想从地址字符串中获取区域列表和地址字符串中存在的区域。我如何在不循环的情况下执行此操作,因为通过循环将需要很多时间。任何解决方案请通过正则表达式或任何其他方法。

  var data = _context.OrderDetails.Where(c => c.ConsignmentId == cId)
            .Select(c => new {address = c.DeliveryAddress, cityId = c.CityId}).ToList();
        string EncryptedConsId = _customEncryptionDecryption.Encode(cId);

        Regex re = new Regex(@"^(.*?(\bkarachi\b)[^$]*)$");
        MatchCollection cityA = re.Matches(data[0].address.ToLower());
        if (cityA.Count != 0)
        {
            var cityNameA = cityA[0].Groups[2];
            if (cityNameA.Value == "karachi")
            {
                var areasForCityA = _context.Areas.Where(a => a.CityId == 1).ToList();
            }
        }

标签: c#asp.netasp.net-coreasp.net-core-webapilis

解决方案


我已经通过这样做解决了我的问题,谢谢大家,非常感谢

 var areas = _context.Areas.Where(a => a.CityId == data[0].cityId).ToList();
        for (int i = 0; i < areas.Count; i++)
        {
            var result = data[0].address.ToLower().Contains(areas[i].Title.ToLower());
            if (result)
            {
                AreaId = areas[i].Id;
                CityId = data[0].cityId;
                var order = _context.OrderDetails.Where(o => o.ConsignmentId == cId).SingleOrDefault();
                order.AreaId = AreaId;
                _context.SaveChanges();
                break;
            }
        }

推荐阅读