locked
Populating Model with Distinct list from an entity list RRS feed

  • Question

  • User1678602988 posted

    [properties] is an Entity List from retrieved from sql server. I also have a Model(not entity framework) called companies and need to populate it with the records contained in [properties].  The defined class in Method 2 fails to return a distinct list, but the anonymous variable in Method 1 does.  My question is Why? and How do I get Method 2 to work?

    METHOD 1 - Does work
    var companies = properties .Select(a => new { AccountID = a.AccountID, AgentCompany = a.AgentCompany, OfficeID = a.OfficeID, AgentName = a.AgentName, AgentEmail = a.AgentEmail, AgentPhone = a.AgentPhone, WebSite = a.AgentWebsite }) .Distinct() .ToList()

    METHOD 2 - Does not work List<Models.Company> companies = properties .Select(a => new Models.Company { AccountID = a.AccountID, AgentCompany = a.AgentCompany, OfficeID = a.OfficeID, AgentName = a.AgentName, AgentEmail = a.AgentEmail, AgentPhone = a.AgentPhone, WebSite = a.AgentWebsite }) .Distinct() .ToList();
    Tuesday, April 12, 2016 2:36 PM

Answers

  • User36583972 posted

    Hi USSKipper,

    On your second method, Distinct method does not work (different objects). You should use GroupBy method. I have made a sample on my side. The following code in my project for your reference.

     protected void Button3_Click(object sender, EventArgs e)
            {
    
                List<Company> properties = new List<Company>();
                properties.Add(new Company() { AccountID = 1, AgentCompany = "Spewx" });
                properties.Add(new Company() { AccountID = 2, AgentCompany = "OKUY" });
                properties.Add(new Company() { AccountID = 2, AgentCompany = "OKUY" });
                properties.Add(new Company() { AccountID = 4, AgentCompany = "HEX" });
    
                //METHOD 1
                var companies = properties
                .Select(a => new
                {
                    AccountID = a.AccountID,
                    AgentCompany = a.AgentCompany,
                })
                .Distinct()
                .ToList();
    
                //METHOD 2
                List<Company> companies2 = properties.GroupBy(r => r.AccountID).Select(a => new Company
                {
                    AccountID = a.Key,
                    AgentCompany = a.Select(r => r.AgentCompany).First()
                }).ToList();
    
                //METHOD 3
                List<Company> companies3 = properties.GroupBy(r => new { r.AccountID, r.AgentCompany}).Select(a =>a.FirstOrDefault()).ToList();
    
                Response.Write(companies.Count.ToString() + "<>" + companies2.Count.ToString());
    
            }
    
    
        public class Company
        {
            public int AccountID { get; set; }
            public string AgentCompany { get; set; }
        }
    

    C# Linq Group By:

    http://stackoverflow.com/questions/5231845/c-sharp-linq-group-by-on-multiple-columns

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 13, 2016 6:49 AM