正确的 LINQ to SharePoint 查询中的 MultiChoice 值

发布于 2024-09-24 12:02:09 字数 789 浏览 2 评论 0原文

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

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

但是,我正在查询的列表,一个 OOTB 任务列表,有许多多选字段(状态、优先级),这些字段被转换为枚举。在我的查询结果中,任务项状态返回为“_2Normal”,而不是我期望的“(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,
}

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

谢谢,魔术安迪。

I am using a LINQ to SharePoint query to return items from a SharePoint list.

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

However, the list I am querying, an OOTB Tasks list, there are a number of multi-choice fields (Status, Priority), which are translated into enumerations. In my query results, a task item status is returned as "_2Normal", and not as "(2) Normal" as I would expect. I see in the proxy file generated by SPMetal.exe that there is a ChoiceAttribute for the task status enumeration which contains the value I require:

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,
}

How can I modify the query above to return the correct value?

Thanks, MagicAndi.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

春风十里 2024-10-01 12:02:09

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

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

Try using this extension method to get the actual string value of your enum.

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();
        }
    }
}
野味少女 2024-10-01 12:02:09

当然,任务项状态将作为 Priority 类型的值返回 - 根本不是字符串。如果您想显示它,我希望您必须将其适当地转换为字符串(可能使用一些辅助方法来记录应用于某些值的属性)。

只需对枚举值调用 ToString() 将返回该值的名称(如果有),否则返回数字的字符串表示形式。它不会关心 ChoiceAttribute。我怀疑这就是这里发生的事情。

Surely the task item status is being returned as a value of type Priority - not a string at all. If you want to display that, I'd expect you to have to convert it into a string appropriately (possibly using some helper methods which take note of the attribute applied to some values).

Just calling ToString() on an enum value will return the name of the value if it has one, or a string representation of the number otherwise. It won't care about ChoiceAttribute. I suspect that's what's happening here.

难理解 2024-10-01 12:02:09

请记住,查询由 SPMetal 默认生成的选择字段不会被转换为 CAML,因此您的任务列表将首先完全加载到内存中,然后进行查询。

简而言之,这意味着随着(并且如果)您的任务列表随着时间的推移而增长,查询的性能也会同样下降。

到目前为止,我还没有找到解决方案(正在努力解决)。

Remember that querying a choicefield, as generated by SPMetal by default, will not be translated to CAML and thus your tasks-list will first be completely loaded to memory, and queried afterwards.

This, in short, means that as (and if) your tasks-list grows in time, performance of the query will drop equally.

I have, so far, not found a solution to it yet (struggling with it).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文