Search code examples
c#linqlambda

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


The following returns Cannot convert lambda expression to type 'bool' because it is not a delegate type

var Products = from s in db.Products where 
          ( from c in s.Manufacturers
  where (x => (from man in model.man where man.HasValue select man.Value).Contains(c.ManufacturerID)
  select c).Any()
  select s;

While this just works

if (model.man != null)
   {
        var students = from s in db.Products
                       where (from c in s.Manufacturers
                       where model.man.Contains(c.ManufacturerID)
                       select c).Any()
                       select s;
   }

What am i doing wrong in the first case? The model.man is declared as

public int?[] man { get; set; }

Solution

  • You can use null conditional access instead (in C# 6)

    var students = from s in db.Products
                   where (from c in s.Manufacturers
                   where model.man?.Contains(c.ManufacturerID) ?? false
                   select c).Any()
                   select s;
    

    Edit: added ?? false because there is no implicit conversion between bool and bool?