0

I have a database of 4000+ objects, most of which are duplicates. I would like to create a new view of them, and shorten the list by showing only 1 instance of each element by the first and last name. I have already created a method in my Repository that I think should work, as well as the button to display my new list. I think The best way to do this is using JavaScript or JQuery, but I'm not sure how to work it out.

This is my Duplicate User sorting method:

    public List<CustomerUserNameDto> GetDuplicateUsers()
    {
        List<CustomerUserNameDto> userList = new List<CustomerUserNameDto>();
        var list = _dbContext.CustomerUsers.ToList();
        foreach (var customer in list )
        {   
            CustomerUserNameDto dto = new CustomerUserNameDto();
            dto.CustomerUserId = customer.CustomerUserId;

            dto.FirstName = customer.FirstName;
            dto.LastName = customer.LastName;

            var check = userList.Find(y => y.FirstName == dto.FirstName && y.LastName == dto.LastName);
            if (check == null)
            {
                userList.Add(dto);
            }


        }

        return userList;
    }

And the JavaScript I have so far:

    function GetDuplicateUserList() {
var serviceURL = 'Main/GetCustomerUserList';
$('#CustomerUserList').html("Loading Data...");
$.ajax({
    type: 'post',
    dataType: 'text',
    cache: false,
    url: serviceURL,
    success: function (data) {
        $('#CustomerUserList').html(data);
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert('Error - ' + errorThrown);
    }
});

}

Any help would be appreciated.

Matt
  • 18
  • 6
  • Help with what? Need to be more specific what your problem is. Also not entirely clear what you are trying to do – charlietfl Apr 09 '15 at 16:56
  • @charlietfl I am currently passing my full db to my duplicates method, and creating a new list that has sorted out the duplicate items. I am now trying to display the new trimmed list to my html table, but my method doesn't seem to be passing it correctly. I would like some help troubleshooting my javascript, and finding any errors or butter functions to use. – Matt Apr 09 '15 at 17:06
  • 1
    can query db for `distinct` values rather than looping over everything in your backend code – charlietfl Apr 09 '15 at 17:10

1 Answers1

0

There is a more efficient way of handling duplicates, as discussed here: https://stackoverflow.com/a/1300116/1845408

You can use DistinctBy from MoreLINQ.

var distinctValues = _dbContext.CustomerUsers.DistinctBy(c => c.CustomerUserId);
Community
  • 1
  • 1
renakre
  • 8,001
  • 5
  • 46
  • 99