Contains Error

S

shapper

Hello,

I have the following Query:

public IQueryable<Tag> GetTags(IList<Tag> tags) {
IQueryable<Tag> _tags = (from t in context.aspnet_Tags
join tn in tags on t.Name equals
tn.Name
select new Tag {
Id = t.TagId,
Created = t.Created,
Name = t.Name,
Updated = t.Updated
}).AsQueryable();
return _tags;

} // GetTags

Basically I receive a list of Tags that contain only the names.
Then I look into the database for tags having the same name.
If exists I get the id, Created and Updated fields and return the
tags.

I am using this method in a few situations. One of them is to check if
all tags inserted by the user exist in the database so I have:

var AllFound = repository.GetTags(tags).Count() == tags.Count;

This is not working an I get the following error:
Local sequence cannot be used in LINQ to SQL implementation of query
operators except the Contains() operator.

I am not sure what I am doing wrong ...

Any idea?

Thank You,
Miguel
 
P

Pavel Minaev

Hello,

I have the following Query:

    public IQueryable<Tag> GetTags(IList<Tag> tags) {
      IQueryable<Tag> _tags = (from t in context.aspnet_Tags
                               join tn intags on t.Name equals
tn.Name
                               select newTag {
                                 Id =t.TagId,
                                 Created = t.Created,
                                 Name = t.Name,
                                 Updated = t.Updated
                               }).AsQueryable();
      return _tags;

    } // GetTags

Basically I receive a list of Tags that contain only the names.
Then I look into the database for tags having the same name.
If exists I get the id, Created and Updated fields and return the
tags.

I am using this method in a few situations. One of them is to check if
all tags inserted by the user exist in the database so I have:

var AllFound = repository.GetTags(tags).Count() == tags.Count;

This is not working an I get the following error:
Local sequence cannot be used in LINQ to SQL implementation of query
operators except the Contains() operator.

"tags" in code above is a List<T>, right? And in GetTags(), you're
trying to join it with a LINQ to SQL Table. It won't work - you cannot
mix collections like that. You can join two Tables (and then it will
generate an SQL JOIN), or you can join two Lists (or, in general,
IEnumerables), and then the join will be performed in-memory, but you
cannot mix.

If you _do_ want an in-memory join, use AsEnumerable() on your Table
so that LINQ to SQL provider doesn't kick in.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top