Linq Query. Please, help. Going crazy ...

S

shapper

Hi,

On a form I have an input where tags are inserted in a CSV format.

Then on my code I convert the CSV string to a List<Tag>. Tag is an
object with two properties: TagID and Name
So when I do the conversion I only fill the Tags names ...

Then I use a join to find these tags into my database.Tags and get
their ID's.

This is what I have:

// Parse form post tags
List<Tag> form = paper.Tags.Split(new char[] {','},
StringSplitOptions.RemoveEmptyEntries).Select(t => new Tag { Name =
t.Trim() }).ToList();

// Define updated post tags
List<Tag> updated = new List<Tag>(from t in database.Tags
join f in form on t.Name
equals f.Name
select t).ToList();

I debugged and the "form" list has the names of the tags I inserted.
And those names do exist in database.Tags.

However, I always get the following error on my second query:
Local sequence cannot be used in LINQ to SQL implementation of query
operators except the Contains() operator.

I have no idea why do I get this. My code seems ok ... I tried
everything I could think of to solve this.

Does anyone knows why do I get this?

Thanks,
Miguel
 
N

Nicholas Paldino [.NET/C# MVP]

Well, the error is pretty self-explainitory. You are querying a
database, and then trying to see if the ids are in the database. LINQ to
SQL doesn't know how to take the local sequence (your list of tags) and turn
that into a list to compare against in the database when it sends the query
over.

In this instance, you might be better off passing your comma-delimited
list to a stored procedure which will parse the list and then return your
result set (looking for matching tags). With SQL Server 2005, you can parse
the list easily with a CLR table-valued function which will return a set of
the tags, and you can join (or check with EXISTS) to see if the tag exists.

Another option would be to generate the lambda expression yourself,
using an OR statement (you can't compose with WHERE since that will produce
an AND expression).
 

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

Similar Threads

List to CSV 3
Linq > Group 2
Local Sequence cannot be used ... except the Contains() operator 4
Linq. Select 2
Linq. Please, need help. 1
Linq. String 5
Linq. Avoid Joins 2
(Collection) 1

Top