按位LINQ结果到字典

时间:2011-07-11 18:43:16

标签: linq-to-sql bit-manipulation todictionary

我需要使用按位运算符查询表以生成字典结果。在谈到LINQ时,我远非专业人士,我有以下几点:

    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())
                                                  });

1 个答案:

答案 0 :(得分:0)

您不能将查询转换为类似的字典,您可以调用ToDictionary函数,它将为您处理所有细节。

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);