首页 > 解决方案 > 避免 ASP.NET MVC5 中的重复记录

问题描述

我是 ASP.NET 的新手,所以我的搜索引擎中有重复记录的问题。当我搜索城镇时,有些记录是重复的,有些是重复的。

图片1

图二

控制器.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-mvcentity-framework

解决方案


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如果列包含脏数据,例如带有前导或尾随空格,这仍然可能失败。


推荐阅读