1

I have three tables in a many to many relationship. These are:

  • t033_region
  • t031_geocode
  • t032_region_to_geocode_mapping

I am using Entity Framework and Repository model with IQueryable GetItems(). How can I retrieve all geocode (t031) entries for a given region id (t033)? I effectively want to say:

select * from t031_geocode where t031_id in (select t031_id from t032_region_to_geocode_mapping where t033_id = @RegionId)

But how do I say this using entity framework and LINQ? I guess it starts with:

var data = _repository.GetItems<t031_geo_code>().Where(g => ???);

But what expression goes in the Where clause to do the above? Or is there a better way to accomplish what I'm doing?

0

2 Answers 2

4

The way I usually do this kind of things is getting first the list of ids that you want to match with and then get the subset of records that match those ids, something like:

var ids = _repository.Get<t032_region_to_geocode_mapping>().Where(x => x.t33_id = @RegionId).Select(x => x.t03_id).ToList();

var data = _repository.Get<t031_geocode>().Where(x => ids.Contains(x.t031_id);
0

It is depending on your repostiory implementation?!

Here it should looking like that:

var regionKeyOrKeys = new int []{XX};
var codes = (from region in t031_geocode.GetAllItems..// Get all items no problem here! because of 
          where (regionKeyOrKeys.Contains(region.Key)) 
          select region).ToList();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.