Linq

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    Linq

    Hello,

    I have the following LINQ query:

    PagedList<Profe ssorPaperpapers = (from p in
    database.Profes sors
    orderby p.Name
    select new ProfessorPaper {
    Professor = p,
    Tags = new List<Tag{
    from pt in
    database.Profes sorTags
    join t in database.Tags
    on pt.TagID equals t.TagID
    where pt.ProfessorID ==
    p.ProfessorID
    select t
    }
    }).ToPagedList( page ?? 1,
    10);

    I am getting the error:
    "The best overloaded Add method
    'System.Collect ions.Generic.Li st<MyApp.Models .Tag>.Add(MyApp .Models.Tag)'
    for the collection initializer has some invalid arguments."

    In "from pt in database.Profes sorTags"

    And the error:
    Argument '1': cannot convert from
    'System.Linq.IQ ueryable<MyApp. Models.Tag>' to 'MyApp.Models.T ag'

    In "select t"

    What am I doing wrong?

    Thanks,
    Miguel



  • Jon Skeet [C# MVP]

    #2
    Re: Linq

    On Jun 30, 5:50 pm, shapper <mdmo...@gmail. comwrote:

    <snip>
    I am getting the error:
      "The best overloaded Add method
    'System.Collect ions.Generic.Li st<MyApp.Models .Tag>.Add(MyApp .Models.Tag)'
    for the collection initializer has some invalid arguments."
    >
    In "from pt in database.Profes sorTags"
    >
    And the error:
    Argument '1': cannot convert from
    'System.Linq.IQ ueryable<MyApp. Models.Tag>' to 'MyApp.Models.T ag'
    >
    In "select t"
    >
    What am I doing wrong?
    Change the curly braces around that expression to parentheses - you
    should be passing in the query expression as a constructor parameter.

    Or get rid of the constructor completely and just call ToList() on the
    query expression.

    Jon

    Comment

    Working...