apprefactory
apprefactory

Reputation: 31

Trouble with LINQ Returning Results Set from Object[] Array Within Object

Consider the following code:

var articlesDisplay = from product in db.ProductSearchData
                      select product.articles;

articlesDisplay = articlesDisplay.Where(a => a[].body.Contains(searchString));

I'm trying to load a results set, but get a compiler error using the array notation in the Where clause. How should I be going about this?

The desired end result is a var articlesDisplay object that can be used in ASP.NET MVC pagination.

Thanks to any/all for your assistance!

Upvotes: 0

Views: 110

Answers (2)

John Wu
John Wu

Reputation: 52290

A lambda expression is just like a function declaration, but instead of method name(paramters){body } it takes the form of parameters => body. So this:

a => a[].body.Contains(searchString)

Is the same as this:

bool Method(Article article)
{
    return article[].body.Contains(searchString);
}

That is obviously not valid, since it won't compile. You need a Func<T,bool>, or a function that accepts a single element and returns true or false depending on whether it is to be included. So you probably want this:

bool Method(Article article)
{
    return article.body.Contains(searchString);
}

Which translates to this:

a => a.body.Contains(searchString).

Upvotes: 1

Bosco
Bosco

Reputation: 1554

remove the array notation

var articlesDisplay = from product in db.ProductSearchData
                      select product.articles;

articlesDisplay = articlesDisplay.Where(a => a.body.Contains(searchString));

Upvotes: 2

Related Questions