0

I have five Models (tables). I want to select all trainings which Venue_Distrcit is accessible by user whose ID is 1. But I prefer if can be solved with .Contains like firstTrainingSet. Also I have practiced with .Foreach but can't fix the proper format.

1. TRAINING 2. TRAINING_VENUE 3. DISTRICT | ID | NAME | VENUE_ID | | ID | NAME | DCODE | | DCODE | NAME | 4. TRAINING_USER 5.TRAINING_USER_DISTRICT_MAPPING |ID | NAME | | ID | USER_ID | DCODE| var TRAINING_USER_ACCESSIBLE_DISTRICT_LIST = (from trainingUserDistrictMappingTable in db.TRAINING_USER_DISTRICT_MAPPING where (trainingUserDistrictMappingTable.TRAINING_USER_ID == 1) select trainingUserDistrictMappingTable).ToList(); var trainingSet = db.TRAINING.Where(x => TRAINING_USER_ACCESSIBLE_DISTRICT_LIST.DCODE .Contains(x.TRAINING_VENUE.DCODE)); 

Here I need trainingSet. And x.TRAINING_VENUE.DCODE means that x is TRAINING parameter and x.TRAINING_VENUE can be fetched because VENUE_ID of TRAINING table is foreign key from VENU table ID, and x.TRAINING_VENUE.DCODE is DOCDE from venue table. Which is actually primary key of DISTRICT table. TRAINING_USER_ACCESSIBLE_DISTRICT_LIST is list from DISTRICT table. The thing I can not do is TRAINING_USER_ACCESSIBLE_DISTRICT_LIST.DCODE.Contains(x.TRAINING_VENUE.DCODE).

13
  • I think problem is that you did not override Equals method in your models so that Contains method will compare fields but not references Commented Feb 29, 2016 at 5:56
  • @Toddams thanks. but i do not want to override model with = signs. I just here want to compare model.field with another same type value. Commented Feb 29, 2016 at 6:00
  • You can implement IEquatable<T> for each of your models without overriding "=" operator Commented Feb 29, 2016 at 6:02
  • 1
    "Also making error" doesn't tell us anything about what that error is. It would be much easier to help you if you would provide a minimal reproducible example. Commented Feb 29, 2016 at 6:52
  • When I am selecting select trainingUserDistrictMappingTable.DCODE that is ok but when only model select trainingUserDistrictMappingTable then can not use .Contains in the model listed data Commented Feb 29, 2016 at 8:53

1 Answer 1

1

As you TRAINING_USER_ACCESSIBLE_DISTRICT_LIST is collection so the expression should be:

var trainingSet = db.TRAINING .Where(x => TRAINING_USER_ACCESSIBLE_DISTRICT_LIST .Any(y => y.DECODE == x.TRAINING_VENUE.DCODE)); 

This will fix your issue.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.