Avatar of ITsolutionWizard
ITsolutionWizard
Flag for United States of America asked on

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
C#LINQ Query

Avatar of undefined
Last Comment
Fernando Soto

8/22/2022 - Mon
zephyr_hex (Megan)

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.
ITsolutionWizard

ASKER
carrierid is associated with two tables

carrier

carriercontact

it is 1 to many relationship

there is no issue on the data side
zephyr_hex (Megan)

So when you do that join in SQL, what do you get?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
zephyr_hex (Megan)

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

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

ITsolutionWizard

ASKER
Your linq returns multi value same as mine
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ITsolutionWizard

ASKER
Basically I want list of Carriers that have contact
ASKER CERTIFIED SOLUTION
Fernando Soto

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question