none
Linq Query. Please, help. Going crazy ... RRS feed

  • Question

  • 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
    Wednesday, August 27, 2008 4:03 PM

Answers

  • You are joining a remote database table (database.Tags) with a local collection of Tag objects (form). L2S will not perform cross domain joins like this.  You can however, use the collection.Contains() method in this fashion and L2S will turn all items in 'collection' into separate parameters for you.

     

    List<Tag> updated =

    (from t in database.Tags

    where form.Contains(t.Name)

    select t).ToList();

     

     

     

    Wednesday, August 27, 2008 4:09 PM
    Moderator

All replies

  • You are joining a remote database table (database.Tags) with a local collection of Tag objects (form). L2S will not perform cross domain joins like this.  You can however, use the collection.Contains() method in this fashion and L2S will turn all items in 'collection' into separate parameters for you.

     

    List<Tag> updated =

    (from t in database.Tags

    where form.Contains(t.Name)

    select t).ToList();

     

     

     

    Wednesday, August 27, 2008 4:09 PM
    Moderator
  • I used your suggestion but I got an error:

    The best overloaded method match for 'System.Collections.Generic.List<MyApp.Models.Tag>.Contains(MyApp.Models.Tag)' has some invalid arguments

    Any idea why?

    Thanks,
    Miguel

    Wednesday, August 27, 2008 6:34 PM
  • I understand that if I use "where form.Contains(t)" it will work but then I am not comparing the names ... in that case I think the tag should be exactly the same, both Name and TagID which are the Tag properties ... or not?

    How to solve this?

    Thanks,
    Miguel
    Wednesday, August 27, 2008 6:42 PM
  • I mistakenly though that 'form' has a collection of names.  I know see that it is tags.  The solution is not to attempt to match tags, but names.

     

    var tagNames = form.Select(t => t.Name).ToList();

     

    List<Tag> updated =

    (from t in database.Tags

    where tagNames.Contains(t.Name)

    select t).ToList();

    Thursday, August 28, 2008 8:22 PM
    Moderator