代码之家  ›  专栏  ›  技术社区  ›  Tangiest

更正LINQ到SharePoint查询中的多选值

  •  3
  • Tangiest  · 技术社区  · 16 年前

    我正在使用LINQ到SharePoint查询从SharePoint列表返回项目。

    var myOpenTasksQuery = from myTasks in tasks
                           where myTasks.TaskStatus != TaskStatus.Completed
                           select myTasks
    

    但是,我正在查询的列表,一个OOTB任务列表,有许多多选字段(Status,Priority),它们被转换为枚举。在我的查询结果中,任务项的状态返回为“\u2normal”,而不是我所期望的“(2)Normal”。我在生成的代理文件中看到SPMetal.exe任务状态枚举有一个ChoiceAttribute,其中包含我需要的值:

    public enum Priority : int {
    
        None = 0,
    
        Invalid = 1,
    
        [Microsoft.SharePoint.Linq.ChoiceAttribute(Value="(1) High")]
        _1High = 2,
    
        [Microsoft.SharePoint.Linq.ChoiceAttribute(Value="(2) Normal")]
        _2Normal = 4,
    
        [Microsoft.SharePoint.Linq.ChoiceAttribute(Value="(3) Low")]
        _3Low = 8,
    }
    

    如何修改上面的查询以返回正确的值?

    谢谢,马吉坎迪。

    3 回复  |  直到 16 年前
        1
  •  1
  •   Jon Skeet    16 年前

    Priority -一根弦也没有。如果您想显示它,我希望您必须将它适当地转换为字符串(可能使用一些helper方法,这些方法会注意应用于某些值的属性)。

    只是打电话而已 ToString() 在枚举中,如果值有名称,则返回值的名称;否则返回数字的字符串表示形式。它不会在乎的 ChoiceAttribute

        2
  •  3
  •   p.campbell    14 年前

    尝试使用此扩展方法获取枚举的实际字符串值。

    foreach (var o in  myOpenTasksQuery)
    {
        Console.WriteLine(o.Priority.StringValueOf());
    }
    
    
    
    public static class Extensions
    {
        public static string StringValueOf(this Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());
            Microsoft.SharePoint.Linq.ChoiceAttribute[] attributes =
                (Microsoft.SharePoint.Linq.ChoiceAttribute[])fi.GetCustomAttributes(
                typeof(Microsoft.SharePoint.Linq.ChoiceAttribute), false);
            if (attributes.Length > 0)
            {
                return attributes[0].Value;
            }
            else
            {
                return value.ToString();
            }
        }
    }
    
        3
  •  1
  •   Kornelis    15 年前

    请记住,默认情况下,SPMetal生成的查询choicefield不会转换为CAML,因此任务列表将首先完全加载到内存中,然后再进行查询。

    简而言之,这意味着随着任务列表的增长,查询的性能也会下降。