Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

lambda

linq

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; }
like image 415
OrElse Avatar asked Sep 02 '25 17:09

OrElse


1 Answers

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?

like image 65
M.kazem Akhgary Avatar answered Sep 05 '25 06:09

M.kazem Akhgary