【问题标题】:Bitwise LINQ result to dictionary按位 LINQ 结果到字典
【发布时间】:2011-07-11 18:43:15
【问题描述】:

我需要使用按位运算符查询表以生成字典结果。在 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())
                                                  });

【问题讨论】:

  • 你有那个查询,它正在做...?
  • 感谢 Skeet 先生的邀请!我改成下面的逻辑。

标签: c# dictionary linq-to-sql bit-manipulation


【解决方案1】:

您不能像这样将查询转换为字典,您可以调用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);

【讨论】:

    猜你喜欢
    • 2012-11-07
    • 1970-01-01
    • 2022-01-16
    • 2010-10-31
    • 2011-06-20
    • 2021-06-16
    • 1970-01-01
    • 2014-12-07
    • 1970-01-01
    相关资源
    最近更新 更多