Jen143
Jen143

Reputation: 835

Select one record from join table

I have this query and i wanted to only select distinct value from Charges table(Port Name must only display once).

public List<Port> GetPortsByCountryOrigin(int countryId, TransportDirection transdirection, TransportType transtype)
    {
        using (var ctx = CreateDbContext())
        {

            return (from item in ctx.Ports
                    join s in ctx.Charges
                    on item.PortId equals s.PortId
                    where (s.TransportDirection == transdirection &&
                    s.TransportType == transtype
                    && item.CountryId == countryId)
                    select item).ToList();
        }
    }

Currently, the Ports.Name are repeating values.

Upvotes: 0

Views: 36

Answers (1)

gabriel
gabriel

Reputation: 64

Try .Distinct() before your ToList()

Upvotes: 1

Related Questions