We help IT Professionals succeed at work.

linq, c#

ITsolutionWizard
on
163 Views
Last Modified: 2017-03-10
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

CERTIFIED EXPERT
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
CERTIFIED EXPERT
Top Expert 2010

Commented:
So when you do that join in SQL, what do you get?
CERTIFIED EXPERT
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?
CERTIFIED EXPERT
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
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions