linq, c#

ITsolutionWizard
ITsolutionWizard used Ask the Experts™
on
Below is my linq / c# and it return duplicated record. All I need to return one "blue shied"
How can I do that?


    public List<Domain.Carrier> GetCarrierList()
        {
            Repository.CarrierInformation.GenericDataContext gdc = new Repository.CarrierInformation.GenericDataContext();
            return (
                    from tblCarriersData in gdc.TblCarriers
                    join tblCarrierContactsData in gdc.TblCarrierContacts
                    on tblCarriersData.CarrierID equals tblCarrierContactsData.CarrierID
                    orderby tblCarriersData.Name
                    select new Domain.Carrier()
                    {
                        name = tblCarriersData.Name,
                        carrierID = tblCarriersData.CarrierID
                    }
                ).ToList();
        }

Open in new window


Result:
Blue Shield
Blue Shield
Blue Shield
Blue Shield
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2010

Commented:
Is CarrierID the field that associates the two tables?

If so, do you have multiple records for Blue Shield in your data?

You're using .ToList(), which tells me that you expect more than one record.  So I suspect the problem is with your data, or you're not using the fields that uniquely identify the association between those two tables.

Author

Commented:
carrierid is associated with two tables

carrier

carriercontact

it is 1 to many relationship

there is no issue on the data side
Top Expert 2010

Commented:
So when you do that join in SQL, what do you get?
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Top Expert 2010

Commented:
Actually, let me take a guess.... by the name of the table TblCarrierContacts, it sounds like that table has contacts associated with the carrier.  That means you're seeing multiple records in your result set because you have multiple contacts for the carrier.

Is there a reason why you're joining the TblCarrierContacts table when the columns you're returning are solely from TblCarriers?
Top Expert 2010

Commented:
Wouldn't this be simpler?

return (
	from tblCarriersData in gdc.TblCarriers
	orderby tblCarriersData.Name
	select new Domain.Carrier()
	{
		name = tblCarriersData.Name,
		carrierID = tblCarriersData.CarrierID
	}
).ToList();

Open in new window

Author

Commented:
Your linq returns multi value same as mine

Author

Commented:
Basically I want list of Carriers that have contact
Retired
Distinguished Expert 2017
Commented:
Hi ITsolutionWizard;

Not near a computer to verify but does this version do what you need?
public List<Domain.Carrier> GetCarrierList()
{
    Repository.CarrierInformation.GenericDataContext gdc = new Repository.CarrierInformation.GenericDataContext();
    return (
          from tblCarriersData in gdc.TblCarriers
          join tblCarrierContactsData in gdc.TblCarrierContacts
          on tblCarriersData.CarrierID equals tblCarrierContactsData.CarrierID
          orderby tblCarriersData.Name
          select new Domain.Carrier()
          {
              name = tblCarriersData.Name,
              carrierID = tblCarriersData.CarrierID
          }
        ).Distinct().ToList();
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial