0

I have a global variable list called objCustomerData I want to be able to return distinct data in GetBranch method.

This is not happening. It is returning all the duplicates. I want it to return just distinct branches.

Please what am I doing wrong here?

        private List<Customer> objCustomerData;

        public ActionResult Index()
        {
            InventoryAssViewModel objModel = new InventoryAssViewModel();
            try
            {   
                 if (countryValue == "CA")
                {
                    objCustomerData = _inventoryService.GetListCountryData("CAA");

                    objModel.BranchModel = GetBranch();
                }
                else 
                {
                    objCustomerData = _inventoryService.GetListCountryData("NIG");                 
                }
            }
            catch (Exception ex)
            {

            }

            return View(objModel);
        }


        public List<Branch> GetBranch()
        {
            List<Branch> filterBranch;

            try
            {
                filterBranch = (from c in objCustomerData
                    select new Branch()
                    {
                        Id = c.Branch,
                        BranchName = c.Branch
                    }).Distinct().ToList();
            }
            catch (Exception)
            {               
                throw;
            }
            return filterBranch;
        }
5
  • blogs.msdn.com/b/csharpfaq/archive/2009/03/25/… Commented Jul 4, 2015 at 2:59
  • 1
    stackoverflow.com/questions/1365748/… Commented Jul 4, 2015 at 2:59
  • This is not helpful. Unable to get this to work. What about groupby? Commented Jul 4, 2015 at 3:31
  • Can you show us what the signature of the class Branch looks like? Commented Jul 4, 2015 at 8:18
  • Unable to get this to work isn't very useful. The link provided by har07 is exactly what you should do. What did you try? Commented Jul 4, 2015 at 9:56

2 Answers 2

1

Try this,

filterBranch = objCustomerData.Select(c => c.Branch).Distinct().Select(c => new Branch()
{
    Id = c,
    BranchName = c
})).ToList();

Or

filterBranch = objCustomerData.GroupBy(x => x.Branch).Select(c => new Branch()
{
    Id = c.Key,
    BranchName = c.Key
})).ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

@user2320476, It is not possible, even GroupBy also returning duplicates? May be the data have spaces or some other characters.
0

You have to either implement IEquatable<T> on the Branch class or, assuming that Branch.Branch is a string, you can utilize the fact that the C# compiler generates both Equals and GetHashCode for the anonymous types:

var filteredBranch = objCustomerData.Select(c =>
 new
 {
     Id = c.Branch,
     BranchName = c.Branch
 }).Distinct().Select(c =>
 new Branch()
 {
     Id = c.Id,
     BranchName = c.BranchName
 }).ToList();

Performance wise the second approach should be slower, because you are creating more objects, first the anonymous objects, then the Branch objects. But it won't make much difference, if you are dealing with a relatively small collection.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.