0

I have an Object, Company, which is a Property on another Object called Load

public Class Load
{
    private Company _Company;
    public Company Company
    {
        get
        {

            return _Company;

        }

        set
        {
            SetPropertyValue("Company", ref _Company, value);
        }
    }
}

I need to get a distinct list of companies when i see the company drop down list. Currently I am seeing duplicates from the database. Can I accomplish this using Linq?

EB.
  • 2,627
  • 8
  • 35
  • 55

1 Answers1

0

Yes, you can do by Linq. I am assuming you have companyID as a primary key.

List lstComp = new List(); var companyids = lstComp.Select(x => x.companyID).Distinct();

  • from question: `I need to get a distinct list of companies`, not *companyids* – EZI Jul 08 '14 at 19:42
  • I am trying this and I getting error: Cannot implicitly convert type List to type Company. here is my code `private Company _Company; public Company Company { get { var distinctCompanies = _Company.Name.Cast() .Where(x => x != null) .Select(x => x.Name) .Distinct() .ToList(); return distinctCompanies; }` – EB. Jul 08 '14 at 21:27
  • If companyid is not primary, then primary key need to be substitute instead of id. It looks like you have Company name that is duplicate, then place companyname instead of id – Ranajit Chatterjee Jul 09 '14 at 20:52