0

how can I simulate the following sql query using linq

Select * From Resource Where Id in
(
Select ResourceId From ResourceRelCategory Where CategoryId =8 
)

note that Resource and Category have Many to Many relationship and ResourceRelCategory is an association table between them. thanks

2 Answers 2

2

You could try something like:

var result = from r in Resource
             where (
                 select c from ResourceRelCategory
                 where c.CategoryId==8
                 select c.ResourceId
             ).Contains(r.Id)
             select r;

or

var result = from r in Resource
             where r.Categories.Any(c => c.Id == 8)
             select r;

or maybee the other way around:

Category.First(c => c.Id == 8).Resources
Sign up to request clarification or add additional context in comments.

5 Comments

the problem is the ResourceRelCategory isn't in mapping files becuase it is just an association entity. there are just Resource and Category entities in Model.
@Vahid: Why don't just add them? If you use automtic generation you can add a "dummy column" so the relation table gets a mapping.
i guess the associatoin table should not be in class model. what do you think?
Mr chfrin, could you help me what do i do if i have a list of dynamic generated category ids when the number of thoes is not specific in desing time.
@Vahid: Try something like where r.Categories.Exists(c => catIds.Contains(c.Id))
0

See the article about this here I have already posted about the same at http://www.codeproject.com/Tips/336253/Filtering-records-from-List-based-similar-to-Sql-I

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.