locked
selecting members from a dictionary with Linq - Operator '&&' cannot be applied to operands of type 'int' and 'bool' RRS feed

  • Question

  • User379720387 posted

    pack.Data.Values contains several dictionary objects.

    It is passed into a method for further processing. In the method there is some linq code that returns a list of endPointIds:

    dp.Select(d => d.EndpointId) 

    Stuff is done with that list of endPoints.

    I need to add an extra condition to that linq code to only get those where the doProcess flag is set to true.

    doProcess is a nullable bool

    dp.Select(d => d.EndpointId && d.doProcess.HasValue)
    
    and also tried
    
    dp.Select(d => d.EndpointId && (d.doProcess ?? true))
    

    Either results in the error message from the title.

    How to correct this?

    TIA

    Thursday, May 9, 2019 8:49 PM

Answers

  • User379720387 posted

    Figured it out: problem was my lack of understanding the difference between SELECT and WHERE in Linq.

    Very well explained here: https://stackoverflow.com/questions/1212746/linq-what-is-the-difference-between-select-and-where

    I was trying to combine a SELECT and WHERE in the same Linq magic.

    This came to light after I broke the functionality into two.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 13, 2019 1:23 AM

All replies

  • User1120430333 posted

    If it really is a Boolean value

    dp.Select.Where(d => d.doProcess) // it's implied that the Boolean value is = true

    or

     dp.Select.Where(d => d.doProcess == true)

    I don't know what d.EndpointId is about in some selection criteria based on a Where clause. 

    Thursday, May 9, 2019 10:52 PM
  • User379720387 posted

    In simple form:

    endPointId / doProcess

    6 / false

    8 / true

    12 / true

    16 / false

    Currently I am getting a list of 6 8 12 16

    What I need is 8 12

    How can I achieve that?

    The dp.Select(d => d.EndpointId) just takes the endPointId from each object.

    Friday, May 10, 2019 1:18 AM
  • User1120430333 posted
    var results = (from a in dp.where(d =>  d.Value == true) select a.Key, a.Value).ToList(); 
    
    foreach(var result in results)
    {
       var key = result.Key;
    var value = result.Value; }

    I don't know try it.

    http://www.java2s.com/Code/CSharp/LINQ/UseLINQwithDictionary.htm

    Friday, May 10, 2019 7:48 AM
  • User379720387 posted

    Figured it out: problem was my lack of understanding the difference between SELECT and WHERE in Linq.

    Very well explained here: https://stackoverflow.com/questions/1212746/linq-what-is-the-difference-between-select-and-where

    I was trying to combine a SELECT and WHERE in the same Linq magic.

    This came to light after I broke the functionality into two.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 13, 2019 1:23 AM