Link to home
Get AccessLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag 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
Avatar of zephyr_hex (Megan)
zephyr_hex (Megan)
Flag of United States of America image

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

ASKER

carrierid is associated with two tables

carrier

carriercontact

it is 1 to many relationship

there is no issue on the data side
So when you do that join in SQL, what do you get?
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?
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

Your linq returns multi value same as mine
Basically I want list of Carriers that have contact
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access