Linq

S

shapper

Hello,

I have the following LINQ query:

PagedList<ProfessorPaper> papers = (from p in
database.Professors
orderby p.Name
select new ProfessorPaper {
Professor = p,
Tags = new List<Tag> {
from pt in
database.ProfessorTags
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.Collections.Generic.List<MyApp.Models.Tag>.Add(MyApp.Models.Tag)'
for the collection initializer has some invalid arguments."

In "from pt in database.ProfessorTags"

And the error:
Argument '1': cannot convert from
'System.Linq.IQueryable<MyApp.Models.Tag>' to 'MyApp.Models.Tag'

In "select t"

What am I doing wrong?

Thanks,
Miguel
 
J

Jon Skeet [C# MVP]

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

In "from pt in database.ProfessorTags"

And the error:
Argument '1': cannot convert from
'System.Linq.IQueryable<MyApp.Models.Tag>' to 'MyApp.Models.Tag'

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
 

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

Linq.How to complete this query? 2
Linq. Please, need help. 1
Linq. Aggregate 4
Linq. String 5
Linq. Avoid Joins 2
Linq > Group 2
Linq. Where 10
Local Sequence cannot be used ... except the Contains() operator 4

Top