Valid MultiChoice Values ​​in LINQ to SharePoint Query

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 requesting is an OOTB Tasks list; there are several fields with several choices (Status, Priority) that translate into enumerations. In my query results, the status of the task item is returned as "_2Normal", and not as "(2) Normal" as I expected. I see in the proxy file generated by SPMetal.exe that there is a ChoiceAttribute list to list the status of the task, which contains the required value:

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.

+3
3

, Priority - . , , (, , , ).

ToString() enum , , . ChoiceAttribute. , .

+1

, .

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

, , SPMetal, CAML, .

, , , , ( ) , .

( ).

+1
source

Source: https://habr.com/ru/post/1766046/


All Articles