Reputation: 5
How can get rid of duplicate and display record(s) using jQuery dataTables in asp.net mvc.
public ActionResult FilterData()
{
var results = db.Cbps.Where(a => a.SDATE == a.SDATE);
List<string> filterdDate = new List<string>();
foreach (var item in results)
{
filterdDate.Add(item.SDATE);
}
var obj = new { data = filterdDate };
//var da = obj;
return Json(obj, JsonRequestBehavior.AllowGet);
}
<script>
$(document).ready(function () {
$('#abc').DataTable({
"ajax": {
"url": "/Filter/FilterData",
"type": "GET",
"datatype": "json"
},
"columns": [
{ "data": "SDATE", "autoWidth": true }
]
});
});
</script>
Results with duplicate
2020-04-18
2020-04-18
2020-04-18
2020-04-18
2020-04-17
2020-04-17
2020-04-17
2020-04-17
Without duplicate
2020-04-18
2020-04-17
Upvotes: 0
Views: 100
Reputation: 26
You can use HashSet to remove duplicates
var filterdDate = new HashSet<string>(results);
var obj = new { data = filterdDate };
return Json(obj, JsonRequestBehavior.AllowGet);
Upvotes: 0
Reputation: 556
You can update your code to do the following:
var filteredDate = db.Cbps.Where(a => a.SDATE == a.SDATE).Select(c => c.SDATE).Distinct();
var obj = new { data = filterdDate };
return Json(obj, JsonRequestBehavior.AllowGet);
The LINQ extension method Distinct() will return unique data.
Upvotes: 1