Dreamcasting
Dreamcasting

Reputation: 444

LINQ: Compare List A and B, if in B, select item in A

I have a list of tags (List A), and a list of tags matched to topics (List B). I want to parse all items in List B and if it's there, have it select the item in List A. I've tried to do a two line and one line statement to do this, but I run into the same problem no matter what I try. Here's my one line attempt of code:

var tags = db.Tags.Where(x=>x.TagID == db.TagLink.Where(y => y.TopicID == incomingTopicID)).ToList();

List A and B have a common column of Tag ID.

Any suggestions?

Update

The structure of db.TagLink are these columns: TagLinkID(Key), TopicID, TagID.

The structure of db.Tags are these columns: TagID, TagName, IsTagScored.

Upvotes: 0

Views: 77

Answers (2)

chomba
chomba

Reputation: 1451

You could use Join, like this:

var tags = db.TagLink.Where(x => x.TopicID == incomingTopicID)
                     .Join(db.Tag, x => x.TagId, y => y.TagId, (x, y) => y)
                     .ToList();

Upvotes: 0

StriplingWarrior
StriplingWarrior

Reputation: 156544

There are several ways you could go about it. Here's one approach:

var tags = db.Tags
    .Where(x=>db.TagLink
        .Any(y => y.TagId == x.TagId && y.TopicID == incomingTopicID))
    .ToList();

Upvotes: 2

Related Questions