Cannot convert System.Collections.Generic.IEnumerable to bool in Linq Lambda expression

Cameron Castillo

I guess this should be straight forward, but what is the correct way of filtering a related table in Linq to SQL. It works when I bring in the secondary table explicitely with a new join, but I'm sure filtering on the related table should also work.

For example:

var q = from p in db.Personnel
        where p.PersonnelGifts.Where(p => p.GiftValue >= 2477)
        select {...}

I get the error that

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'bool'

Paweł Hemperek
var q = from p in db.Personnel
    where p.PersonnelGifts.Where(p => p.GiftValue >= 2477).Any()
    select {...}

or as @Jon Skeet pointed out - .Any() also accepts predicate, so you can write it like

var q = from p in db.Personnel
    where p.PersonnelGifts.Any(p => p.GiftValue >= 2477)
    select {...}

Why your code didn't work? .Where() returns IEnumerable (or IQueryable for that matter) so you can chain it with another LINQ methods which accepts IEnumerable as parameter. Your where clause expects bool value and exactly this type is returned by .Any() method.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

"Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<C3S.Models.Permission>' to 'bool'."

From Dev

Cannot implicitly convert type System.Linq.IQueryable to System Collections.Generic.IEnumerable

From Dev

Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject' to 'System.Collections.Generic.IEnumerable<Employee>'

From Dev

Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject' to 'System.Collections.Generic.IEnumerable<Employee>'

From Dev

cannot convert from 'System.Collections.Generic.IEnumerable<char>' to 'string'

From Dev

cannot convert from 'string' to 'System.Collections.Generic.IEnumerable<string>

From Dev

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable

From Dev

Cannot implicitly convert type ‘System.Linq.IQueryable<AnonymousType#1>’ to ‘System.Collections.Generic.IEnumerable<SubCateViewModel>’

From Dev

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Collections.Generic.ICollection

From Dev

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable To System.Collections.Generic.IEnumerable<TiendaDeportes.producto>

From Dev

Cannot implicitly convert type 'Task<System.Collections.Generic.IEnumerable<IClass>>' to 'System.Collections.Generic.IEnumerable<IClass>

From Dev

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<T>

From Dev

Cannot implicitly convert type System.Collections.Generic.List<IEnumerable> to <IEnumerable

From Dev

Operator '&&' cannot be applied to operands of type 'bool' and 'System.Collections.Generic.IEnumerable<Feature>'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<String>' to 'System.Collections.Generic.IEnumerable<turon.Model.Products_Products>

From Dev

"Cannot convert from bool to system.collections.generic.list<bool>" In what seems to be good code

From Dev

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Web.Mvc.ActionResult'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Web.Mvc.ActionResult'

From Dev

cannot convert to 'System.Collections.IEnumerable

From Dev

Cannot implicity convert type System.Linq.Expression<System.Func<Object, bool>> to bool

From Dev

how to solve the following error: cannot convert from 'int' to 'System.Collections.Generic.IEnumerable<int>?

From Dev

C#, cannot convert from 'System.Collections.Generic.IEnumerable' to 'string[]'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<string> to string

From Dev

C#: Error-Cannot implicitly convert type 'int' to 'System.Collections.Generic.IEnumerable<double>'

From Dev

Cannot implicitly convert type 'void' to 'System.Collections.Generic.Dictionary<string,bool>

From Dev

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List'

From Dev

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List

From Dev

can not implicitly convert type System.Collections.Generic.Ienumerable<string> to system.Linq.IOrderedEnumerable<AnonymousType#1>

From Dev

cannot implicitly convert type system.collection.generic IEnumerable to models in group by linq c#

Related Related

  1. 1

    "Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<C3S.Models.Permission>' to 'bool'."

  2. 2

    Cannot implicitly convert type System.Linq.IQueryable to System Collections.Generic.IEnumerable

  3. 3

    Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject' to 'System.Collections.Generic.IEnumerable<Employee>'

  4. 4

    Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject' to 'System.Collections.Generic.IEnumerable<Employee>'

  5. 5

    cannot convert from 'System.Collections.Generic.IEnumerable<char>' to 'string'

  6. 6

    cannot convert from 'string' to 'System.Collections.Generic.IEnumerable<string>

  7. 7

    Cannot implicitly convert type 'System.Collections.Generic.IEnumerable

  8. 8

    Cannot implicitly convert type ‘System.Linq.IQueryable<AnonymousType#1>’ to ‘System.Collections.Generic.IEnumerable<SubCateViewModel>’

  9. 9

    Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Collections.Generic.ICollection

  10. 10

    Cannot implicitly convert type 'System.Collections.Generic.IEnumerable To System.Collections.Generic.IEnumerable<TiendaDeportes.producto>

  11. 11

    Cannot implicitly convert type 'Task<System.Collections.Generic.IEnumerable<IClass>>' to 'System.Collections.Generic.IEnumerable<IClass>

  12. 12

    Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<T>

  13. 13

    Cannot implicitly convert type System.Collections.Generic.List<IEnumerable> to <IEnumerable

  14. 14

    Operator '&&' cannot be applied to operands of type 'bool' and 'System.Collections.Generic.IEnumerable<Feature>'

  15. 15

    Cannot implicitly convert type 'System.Collections.Generic.List<String>' to 'System.Collections.Generic.IEnumerable<turon.Model.Products_Products>

  16. 16

    "Cannot convert from bool to system.collections.generic.list<bool>" In what seems to be good code

  17. 17

    Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Web.Mvc.ActionResult'

  18. 18

    Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Web.Mvc.ActionResult'

  19. 19

    cannot convert to 'System.Collections.IEnumerable

  20. 20

    Cannot implicity convert type System.Linq.Expression<System.Func<Object, bool>> to bool

  21. 21

    how to solve the following error: cannot convert from 'int' to 'System.Collections.Generic.IEnumerable<int>?

  22. 22

    C#, cannot convert from 'System.Collections.Generic.IEnumerable' to 'string[]'

  23. 23

    Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<string> to string

  24. 24

    C#: Error-Cannot implicitly convert type 'int' to 'System.Collections.Generic.IEnumerable<double>'

  25. 25

    Cannot implicitly convert type 'void' to 'System.Collections.Generic.Dictionary<string,bool>

  26. 26

    Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List'

  27. 27

    Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List

  28. 28

    can not implicitly convert type System.Collections.Generic.Ienumerable<string> to system.Linq.IOrderedEnumerable<AnonymousType#1>

  29. 29

    cannot implicitly convert type system.collection.generic IEnumerable to models in group by linq c#

HotTag

Archive