8

I'm building a page that shows a LINQ query result as a table.

  1. Setup the base query in the 'SetupArticleQuery()' method which saves the query to 'this.articles'.
  2. Run another method, 'UpdateFilter()' to do some filtering on the results that are saved in 'this.articles'.

I get the error

Cannot convert lambda expression to type 'string' because it is not a delegate type

at the line with the code

this.articles = from art in this.articles
                where art.category_id == this.categoryId
                select art;

Any ideas how to fix the code below?

namespace WebApplication3 {
    public partial class _Default : System.Web.UI.Page {
        private IQueryable articles;

        protected void Page_Load(object sender, EventArgs e) {
            this.SetupArticleQuery();
            this.UpdateFilter();
        }

        private void SetupArticleQuery() {
            this.articles = from a in KB.Articles
                            join t in KB.Teams on a.primary_team_id equals t.id
                            join cat in KB.Categories on a.category_id equals cat.id
                            join scat in KB.SubCategories on a.subcategory_id equals scat.id
                            join top in KB.Topics on a.topic_id equals top.id
                            select new {
                                a.id,
                                a.title,
                                a.view_count,
                                a.created_at,
                                a.created_by,
                                a.primary_team_id,
                                primary_team_name = t.name,
                                category_id = cat.id,
                                category_name = cat.name,
                                subcategory_id = scat.id,
                                subcategory_name = scat.name,
                                topic_id = top.id,
                                topic_name = top.name
                            };
        }

        private void UpdateFilter() {
            if (this.categoryId > 0) {
                this.articles = from art in this.articles
                                where art.category_id == this.categoryId
                                select art;

            }
        }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Damiro
  • 251
  • 4
  • 5
  • 14

4 Answers4

28

I had to add the following to get this error to go away.

using System.Data;
using System.Data.Entity;
Asa Julian
  • 281
  • 3
  • 2
13

Actually I haven't found any problem in your code.

From this answer, I would suggest to confirm that you have added:

Using System.Linq;

Good luck!

Community
  • 1
  • 1
Homam
  • 23,263
  • 32
  • 111
  • 187
5

This query:

this.articles = from a in KB.Articles
                join t in KB.Teams on a.primary_team_id equals t.id
                join cat in KB.Categories on a.category_id equals cat.id
                join scat in KB.SubCategories on a.subcategory_id equals scat.id
                join top in KB.Topics on a.topic_id equals top.id
                select new {
                    a.id,
                    a.title,
                    a.view_count,
                    a.created_at,
                    a.created_by,
                    a.primary_team_id,
                    primary_team_name = t.name,
                    category_id = cat.id,
                    category_name = cat.name,
                    subcategory_id = scat.id,
                    subcategory_name = scat.name,
                    topic_id = top.id,
                    topic_name = top.name
                };

Won't work like this since it returns an anonymous type. As a result, you'd have to type it as var, which is invalid for class-level members; you can only use var for local variables.

What you need to do is create an actual class to hold your projection, and have something like:

...
join top in KB.Topics on a.topic_id equals top.id
select new LocalDTO()
{
    id = a.id,
    ...
};

From there you could just have:

private void UpdateFilter()
{
    if (this.categoryId > 0)
        this.articles = this.articles.Where(a => art.category_id);
}

And of course this.articles would be declares as IQueryable<LocalDTO>.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
1

If it is a problem with anonymous types, can you do something like this?

private void UpdateFilter() {
    if (this.categoryId > 0) {
        this.articles = this.articles.Where(a => a.category_id == this.categoryId).AsQueryable();
    }
}

This may be off base, since the underlying lambda is the same.

Josh Anderson
  • 5,975
  • 2
  • 35
  • 48