-1

i've tried to do this

model class is:

public class Model
{
    public string Title{ get; set; }
    public int PartId { get; set; }
    public bool Valid { get; set; }    
}

With this i get all films from db:

 List<Model> movies = this._context.ExecuteQuery<Model>(querystring).ToList();

I've tried to get all movie which titles contains (not equal) the word "monster"

List<Model> results = movies.Where(c=> c.Title.Contains("monster"));
Hardik Patel
  • 67
  • 1
  • 16
  • 4
    We need more information. Is it LinqToSql or EntityFw ? – Fabjan Nov 05 '18 at 10:41
  • 4
    What exactly is `movies`? It's obviously not a simple `List` but some kind of query of a queryprovider like Linq2Sql or EntityFramework... – René Vogt Nov 05 '18 at 10:42
  • The code, as is, does not compile. `List results = movies.Where(c=> c.Title.Contains("monster"));` Given the answer you have marked, I have removed the incorrect part of your question (which stated you had an exception, when in reality you have a compile time error). – mjwills Nov 05 '18 at 10:59
  • Possible duplicate of [Trying to understand how linq/deferred execution works](https://stackoverflow.com/questions/25781982/trying-to-understand-how-linq-deferred-execution-works) – mjwills Nov 05 '18 at 11:04

1 Answers1

2

List<Model> results = movies.Where(c=> c.Title.Contains("monster"));

is incomplete. You need to add an operator after using Where() - in your case, you maybe want .ToList():

List<Model> results = movies.Where(c=> c.Title.Contains("monster")).ToList();

(or change your List<Model> to IEnumerable<Model>, or even var)

mjwills
  • 23,389
  • 6
  • 40
  • 63
dognose
  • 20,360
  • 9
  • 61
  • 107
  • 3
    Though it's correct that OP is missing a `.ToList()`, this does not explain the error message OP receives. – René Vogt Nov 05 '18 at 10:43
  • @RenéVogt Maybe, because he tries to evaluate the expression within visual studio? (See https://stackoverflow.com/questions/36559399/lambda-expressions-in-immediate-window-for-vs2015) (Only a guess, its nowhere mentioned) – dognose Nov 05 '18 at 10:47
  • 2
    @davidelondei because the return type of `where()` is `IEnumerable`. Not all `IEnumerable` are `List`, hence you either need to work with `IEnumerable` or call `ToList(), ToArray(), First(), FirstOrDefault(),....` to either get an `List, Array or T,...`. – dognose Nov 05 '18 at 11:11
  • also you maybe want to use `c.Title.ToLower().Contains("monster")`, if your search should be case-insensitive. – dognose Nov 05 '18 at 11:19
  • 2
    Please be aware, that every time you call `.ToList()` you actually send a request to your database. That might not be what you want. For example if you 'prepare' a query, which is some lines under it used to join a different table. Try to send queries as little as possible. – nilsK Nov 05 '18 at 12:17
  • 1
    @nilsK In this specific instance, the change that dognose proposed would not send a query to the database (since `movies` is already a `List`). – mjwills Nov 06 '18 at 00:07
  • @mjwills yes, you are right. Thanks for mentioning! – nilsK Nov 06 '18 at 08:17