Using enum with a flag in an Entity Framework request

I have an enum type as shown below

  [Flags]
    public enum WeekDays
    {

        Monday = 1,
        Tuesday = 2,
        Wednesday = 4,
        Thursday = 8,
        Friday = 16,
        Saturday = 32,
        Sunday = 64,
    }


   WeekDays dw = WeekDays.Friday | WeekDays.Monday | WeekDays.Saturday;

   int dwInt = (int)dw;

   var list = query.Where(f => f.FromDateTime.DayOfWeek == dwInt "??????????? what can i do there????").ToList();
+5
source share
4 answers

is here

 public static Dictionary<int, int> map = new Dictionary<int, int>() { { 0, 64 }, { 1, 1 }, { 2, 2 }, { 3, 4 }, { 4, 8 }, { 5, 16 }, { 6, 32 } };

//actually,this gets from a user interface,but we shoul be focus database layer
WeekDays[] dw = new WeekDays[] {WeekDays.Saturday,WeekDays.Tuesday,WeekDays.Wednesday };

int[] systemDayOfWeekList = new int[daysOfWeek.Length];

for (int i = 0; i < daysOfWeek.Length; i++)
{
  systemDayOfWeekList[i] = map.FirstOrDefault(e => e.Value == (int)daysOfWeek[i]).Key;
}

query = query.Where(f => dayOfWeekList.Contains(((int)SqlFunctions.DatePart("dw", f.FromDateTime))));
+2
source

I am going to assume that you were not sure what to add the query to the filter for the days that you specified in the source.

, , , dwInt , DayOfWeek "set", . , , , - , "" AND DayOfWeek dwInt, , 0, , "" . , :

var list = query.Where(f => (f.FromDateTime.DayOfWeek & dwInt) >0).ToList()

, , .

+8

Entity Framework 6.1, HasFlag .

:

query.Where(f => f.FromDateTime.DayOfWeek.HasFlag(WeekDays.Friday | WeekDays.Monday)).ToList();

. https://entityframework.codeplex.com/workitem/1497.

+8

[HasFlags] , , .ToString() (AFAIK) Enum.Parse(). , [HasFlags]. enums EF , , : int64, int32, int16, byte, sbyte (: EDM, , , ). , , (, EF), enum, ( , - , ). , , , ???? , (Sql Server )

+6
source

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


All Articles