c# - 如何将区域列表与“地址”(字符串)匹配并获取列表和字符串中都存在的区域 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();
}
}
解决方案
我已经通过这样做解决了我的问题,谢谢大家,非常感谢
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;
}
}
推荐阅读
- php - 使用核心 php 在 Firebase 上创建动态链接
- linux - 构建Shadowsocks android github代码时出错
- regex - 如何在 TDIPerlRegEx 正则表达式中设置不贪婪的选项
- c++ - ( var > x) 和 ( x < var) 之间有什么区别?
- scala - 如何在 Akka HTTP 中嵌套路由?
- angular - 错误错误:找不到类型为“对象”的不同支持对象“[对象对象]”
- php - 将表 PHP 中的变量传递给引导模式
- java - 无法读取 biz.aQute.bnd:bnd-maven-plugin:jar:3.3.0 的工件描述符:
- jmeter-4.0 - 如何在 Jmeter 的负载测试期间保存值并将它们写入外部 csv 文件
- javascript - jQuery draggable : 只能在一个 droppable 上拖动