asp.net-mvc - 避免 ASP.NET MVC5 中的重复记录
问题描述
我是 ASP.NET 的新手,所以我的搜索引擎中有重复记录的问题。当我搜索城镇时,有些记录是重复的,有些是重复的。
控制器.cs
[Authorize(Roles = "Attorney")]
public ActionResult CreateNext(int? id)
{
var currentUser = manager.FindById(User.Identity.GetUserId());
var nfDoc = db.NFDOCUMENTS.Find(id);
if (nfDoc.UserId == currentUser.Id && nfDoc.FacilityId == null)
{
var contacts = db.Contacts.Where(x => x.ContactCategory.Name == "Facility");
List<string> towns = new List<string>();
foreach (var item in contacts)
{
if (!towns.Contains(item.City))
{
towns.Add(item.City);
}
}
ViewData["towns"] = towns;
var medProviders = db.Contacts.Where(x => x.ContactCategory.Name == "Facility" && x.Firstname != null).ToList();
ViewData["medProviders"] = medProviders;
var pat = db.Patients.Where(x => x.Id == nfDoc.PatientId).FirstOrDefault();
ViewBag.Address = pat.Address1 + ", " + pat.City + ", " + pat.State + ", " + pat.Zip;
ViewBag.InsuranceId = new SelectList(db.Contacts.Where(s => s.ContactCategory.Name == "Insurance Carrier"), "Id", "Firstname");
ViewBag.AdjusterId = new SelectList(db.Contacts.Where(s => s.ContactCategory.Name == "Adjuster"), "Id", "Firstname");
ViewBag.FacilityId = new SelectList(db.Contacts.Where(s => s.ContactCategory.Name == "Facility"), "Id", "Firstname");
ViewBag.DoctorId = new SelectList(db.Contacts.Where(s => s.ContactCategory.Name == "Doctor"), "Id", "Firstname");
ViewBag.PatientId = pat.Id;
ViewBag.PatientName = pat.Firstname + " " + pat.Lastname;
return View();
}
else
{
return RedirectToAction("Create");
}
}
看法
所以我希望在搜索引擎过滤城镇之后,我想避免重复
<div class="input-group col-md-12">
<input id="search" type="text" class="form-control input-lg" placeholder="Search towns" />
</div>
<ul class="list-group nav nav-pills nav-stacked" style="height: 200px; overflow-x: hidden; overflow-y: auto">
<li><a class="reload-towns"><i class="icon-location4"></i> ALL TOWNS</a></li>
@foreach (var item in towns)
{
<li><a class="town" data-town="@item"><i class="icon-location3"></i> @item</a></li>
}
</ul>
</div>
</div>
</div>
解决方案
ASP.NET 是一个 Web 框架。它与数据访问无关。数据访问是Entity Framework的工作。
您没有解释哪个查询返回重复项,但我怀疑每个城市您有多个联系人。这个循环:
foreach (var item in contacts)
{
if (!towns.Contains(item.City))
{
towns.Add(item.City);
}
}
使用 .NET 比较规则,这意味着大小写和空格很重要。
可以改写为
var towns=db.Contacts.Where(x => x.ContactCategory.Name == "Facility")
.Select(x=>x.City)
.Distinct()
.ToList();
这将生成一个如下所示的查询:
SELECT DISTINCT City
FROM Contacts inner join ContactCategory on ContactCategory.ID=Contacts.CategoryID
Where ContactCategory.Name='Facility';
这只会唯一的城市名称并在列表中返回结果。字符串的大小写是否重要取决于City
列的排序规则,但最常见的选项是使用不区分大小写的排序规则。
City
如果列包含脏数据,例如带有前导或尾随空格,这仍然可能失败。
推荐阅读
- reactjs - 状态属性在反应 + ts 中的 onChange 上不可用?
- ios - UICollectionView 显示在模拟器上,但不显示在实际设备上
- javascript - 如何在 RowDataPacket 中循环数据
- python - Python:两个整数不相等
- python - 在 CentOS 服务器上运行时,Python 脚本在 smptlib.SMTP(server) 处冻结
- python - 无论如何我可以在气流中设置我的代码运行的工作目录吗?
- ios-charts - PieChartView 中带有中心文本的半透明孔
- javascript - React 不会访问嵌套状态
- json - 如何在飞镖颤振中将json字符串转换为json对象?
- woocommerce - 通过 API 从 WooCommerce 向外部系统发送订单详细信息