Search code examples
c#dictionarylinq-to-sqlbit-manipulation

Bitwise LINQ result to dictionary


I need to query a table using the bitwise operator to produce a dictionary result. I'm far from a pro when it comes to LINQ, I have the following:

    return (Dictionary<string, bool>) (from r in db.LocationVisibilities
                                       where (r.Code & (int) permissionLevel) != 0 //bitwise statement in where clause
                                       select new
                                                  {
                                                      r.Item, value = Boolean.Parse(r.Attribute.ToString())
                                                  });

Solution

  • You can't cast a query to a dictionary like that, you can call the ToDictionary function instead and it will handle all the details for you.

    var dict=
       (from r in db.LocationVisibilities
        where (r.Code & (int) permissionLevel) != 0 //bitwise statement in where clause
        select new {
          r.Item, value = Boolean.Parse(r.Attribute.ToString())
        }).ToDictionary(w=>w.Item, w=>w.value);