-5

我需要在enum DaysOfTheWeek过滤后的值中找到List<int> daysId并返回过滤后的枚举

我的枚举:

[Flags]
public enum DaysOfTheWeek
{
    Sunday = 1,
    Monday = 2,
    Tuesday = 4,
    Wednesday = 8,
    Thursday = 16,
    Friday = 32,
    Saturday = 64,
    AllDays = 127,
}

-

var allValues = Enum.GetValues(typeof(DaysOfTheWeek));

我的整数列表:

List<int> daysId = new List<int> {1,3};
4

3 回答 3

1
var filtered = daysId.Select(d => (DaysOfTheWeek)d);
于 2013-04-15T13:08:59.220 回答
0

不确定你的问题。像这样的东西?

var filtered  = daysId.Select(d=>(DaysOfTheWeek)(1<<(d-1))).ToList();
于 2013-04-15T13:39:05.903 回答
0

也许我太复杂了,但看看这个:

int[] daysId = new int[] { 1, 3,48,127 };

Dictionary<int, List<DaysOfTheWeek>> result = new Dictionary<int, List<DaysOfTheWeek>>();

foreach (int id in daysId)
{
    result[id] = new List<DaysOfTheWeek>();
    BitArray ba = new BitArray(new int[]{id});
    for (int i=0;i<ba.Length;i++)
    {
        if (ba[i])
        {
            result[id].Add((DaysOfTheWeek)(1 << i));
            Console.WriteLine((DaysOfTheWeek)(1 << i));
         }                    
     }
    Console.WriteLine("id-" + id + " > " + (DaysOfTheWeek)id);                
}

Console.ReadKey();

检查输出,看看它是否是你想要的。如果需要,您可以使用直接转换为星期几,(DaysOfTheWeek)id或者您可以单独获取每一个DaysOfTheWeek。上面两个我都做了。

于 2013-04-15T13:45:31.213 回答