How do I fulfill this request using L2E?

I have 2 tables:

Activities       ActivityKeywords
**********       ****************
ID         -->   ActivityID
Name             Keyword

      

I need to return all actions that match a specific keyword.

+2


source to share


4 answers


var q = from a in Context.Activities
        where a.Keywords.Any(k => k.Keyword == someKeyword)
        select a;

      



As I said in the comments, it's almost always wrong to use join in LINQ for Entities. Use relationship properties instead.

+5


source


check out Craig Stuntz's answer for a cleaner way if you have an attitude defined

My previous answer was wrong, but it works for me.



var activities = from a in db.Activities 
                 join ak in db.ActivityKeywords on a.ID equals ak.ActivityID 
                 where ak.Keyword == "yourkeyword" 
                 select a;

      

+1


source


I think you need something like

Give me all Activities which ID are in a list of ActivitiyKeywords.ID's

      

If this is your question, you can try this:

var ids = from k in db.ActivityKeywords select k.ActivityID;

var result = from a in db.Activities where ids.Contains(a.ID) select a;

      

More information here .

0


source


var results = (from a in Activities
               from k in ActivityKeywords
               where k.Keyword == "keyword" && k.ActivityID == a.ID
               select a).Distinct();

      

0


source







All Articles