linq, c#

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
LVL 1
ITsolutionWizardAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zephyr_hex (Megan)DeveloperCommented:
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.
0
ITsolutionWizardAuthor Commented:
carrierid is associated with two tables

carrier

carriercontact

it is 1 to many relationship

there is no issue on the data side
0
zephyr_hex (Megan)DeveloperCommented:
So when you do that join in SQL, what do you get?
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

zephyr_hex (Megan)DeveloperCommented:
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?
0
zephyr_hex (Megan)DeveloperCommented:
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

0
ITsolutionWizardAuthor Commented:
Your linq returns multi value same as mine
0
ITsolutionWizardAuthor Commented:
Basically I want list of Carriers that have contact
0
Fernando SotoRetiredCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.