Local Sequence cannot be used ... except the Contains() operator

S

shapper

Hello,

I am getting an error:
NotSupportedException was unhandled by user code.
Local Sequence cannot be used in Linq to SQL implementation of query
operators except the Contains() operator.

On the Linq query:

List<PostsTag> ties = (from t in database.Tags
join pt in paper.Tags on t.Name equals
pt.Name
select new PostsTag {
PostID = paper.Post.PostID,
TagID = t.TagID
}).ToList();

Basically Tag has two properties: TagID and Name.

paper.Tags is a List<Tag> where each Tag has only the Name defined.
So I need to pick the TagID from database.Tags by finding which Tag
given the name.

I think the problem is that the join in theory can give me more than
one record.
However, I know that there aren't two tags with same Name as I test it
before I insert a new tag.

How can I solve this without having to introduce a Sub Query?

Thanks,
Miguel
 
P

Pavel Minaev

I am getting an error:
NotSupportedException was unhandled by user code.
Local Sequence cannot be used in Linq to SQL implementation of query
operators except the Contains() operator.

On the Linq query:

      List<PostsTag> ties = (from t in database.Tags
                             join pt in paper.Tags on t.Name equals
pt.Name
                             select new PostsTag {
                               PostID =paper.Post.PostID,
                               TagID = t.TagID
                             }).ToList();

What is "database" here, and what is "paper"?

What happens if you reverse the join order (i.e., "from pt ... join
t")?
 
S

shapper

What is "database" here, and what is "paper"?

What happens if you reverse the join order (i.e., "from pt ... join
t")?

database.Tags is a LinqToSQL data context:
private MyAppDataContext database = new MyAppDataContext();

If I reverse the join I get:
The name 'pt' is not in scope on the left side of 'equals'. Consider
swapping the expressions on either side of 'equals'.

paper is an instance of class PostPaper:

public class PostPaper {
public Post Post { get; set; }
public List<Tag> Tags { get; set; }
public string TagsCSV { get; set; }
}

and paper.Tags is a list of Tags created as follows:
paper.Tags = paper.TagsCSV.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries).Select(t => new Tag { Name =
t.Trim() }).ToList();

Thank you,
Miguel
 
S

shapper

What is "database" here, and what is "paper"?
What happens if you reverse the join order (i.e., "from pt ... join
t")?

database.Tags is a LinqToSQL data context:
private MyAppDataContext database = new MyAppDataContext();

If I reverse the join I get:
The name 'pt' is not in scope on the left side of 'equals'.  Consider
swapping the expressions on either side of 'equals'.

paper is an instance of class PostPaper:

  public class PostPaper {
    public Post Post { get; set; }
    public List<Tag> Tags { get; set; }
    public string TagsCSV { get; set; }
  }

and paper.Tags is a list of Tags created as follows:
      paper.Tags = paper.TagsCSV.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries).Select(t => new Tag { Name =
t.Trim() }).ToList();

Thank you,
Miguel

Please, anyone?
 
P

Pavel Minaev

What is "database" here, and what is "paper"?
What happens if you reverse the join order (i.e., "from pt ... join
t")?

database.Tags is a LinqToSQL data context:
private MyAppDataContext database = new MyAppDataContext();

If I reverse the join I get:
The name 'pt' is not in scope on the left side of 'equals'.  Consider
swapping the expressions on either side of 'equals'.

paper is an instance of class PostPaper:

  public class PostPaper {
    public Post Post { get; set; }
    public List<Tag> Tags { get; set; }
    public string TagsCSV { get; set; }
  }

and paper.Tags is a list of Tags created as follows:
      paper.Tags = paper.TagsCSV.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries).Select(t => new Tag { Name =
t.Trim() }).ToList();

Your problem is that you're trying to join an SQL table and an in-
memory list. Since you write the query against the SQL table, it goes
through LINQ to SQL, which rightly complains that it cannot do that.
If you really want to do an in-memory join, use
"database.Tags.AsEnumerable()" instead of just "database.Tags".
 

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