Ando
Ando

Reputation: 11429

Select EF entities that have all the given Tags (where tag is an EF entity)

I have the following situation: An "Conversations" entity/table which has multiple Tags associated to it. Tag is also an entity/table - the key/id is tagName (a string).

On the client side (javascript) I work with arrays of string when dealing with Tags.

Now I want want to retrieve all the conversations that have all given tags.


The input is an array of strings and the output should be a collection of Conversations

I've tried:

var filterTags = new List<EFModels.Tag>();
foreach (var tagName in tags)
            {
               filterTags.Add(new EFModels.Tag() { Name = tagName});
            }

var conversations = from c in context.Conversations where !c.Tags.Except(filterTags).Any() select c ;

The problem with this is: Unable to create a constant value of type 'EFModels.Tag'. Only primitive types or enumeration types are supported in this context - which makes sense.

Now how can I do my select? What's the best approach? (I still want to use linq and not write the sql select)

Upvotes: 3

Views: 527

Answers (1)

Nas
Nas

Reputation: 1121

You can project c.Tags to tag name.

I guess it will look like that

var conversations = from c in context.Conversations where !c.Tags.Select(tag => tag.Name ).Except(filterTags).Any() select c ;

Where filterTags is the list of strings containing the names of the tags

Upvotes: 1

Related Questions