避免在ASP.NET MVC5中重复记录

时间:2018-08-07 08:43:33

标签: asp.net-mvc entity-framework

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

Picture 1

Picture 2

Controller.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>

2 个答案:

答案 0 :(得分:3)

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列包含脏数据(例如,前导或尾随空格),则此操作仍可能失败。

答案 1 :(得分:-1)

如果要在c#端解决此问题,则可以始终使用HashSet(哈希集集合仅在不重复的情况下向集合中添加另一个字符串)。

所以不要使用它:

List<string> towns = new List<string>();

使用:

HashSet<string> towns = new HashSet<string>();

从我的角度来看,最好在SQL方面(实体框架)解决此问题:

var towns = db.Contacts.Where(x => x.ContactCategory.Name == "Facility").Select(m => m.City).Distinct().ToList();