Brij
Brij

Reputation: 13151

More than 2 tables JOIN in LINQ, scope issue

I am trying to join 3 table in LINQ.

var fromCities = from c in cityRepository.Cities
                             join r in routeRepository.Routes on c.Id equals r.FromCityId
                             join cr in rentDetailRepository.CarRentDetails on cr.CityId equals c.Id
                             select c;

I am getting that cr and c do not exist in the second join statement? Could somebody help?

Upvotes: 0

Views: 2969

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460238

Change the order in the last join:

...
join cr in rentDetailRepository.CarRentDetails on c.Id equals cr.CityId 

join clause (C# Reference)

A join clause performs an equijoin. In other words, you can only base matches on the equality of two keys. Other types of comparisons such as "greater than" or "not equals" are not supported. To make clear that all joins are equijoins, the join clause uses the equals keyword instead of the == operator. The equals keyword can only be used in a join clause and it differs from the == operator in one important way. With equals, the left key consumes the outer source sequence, and the right key consumes the inner source. The outer source is only in scope on the left side of equals and the inner source sequence is only in scope on the right side.

Upvotes: 3

Related Questions