0

I am trying to map a property to the database. However it is a list/array for which I know the fixed length. So I would like those items to be mapped into the same database table without needing to be mapped into a different database table.

public class Recurrency{

    public int Id { get; set; }

    public DateTime BeginDate { get; set; }

    public DateTime EndDate { get; set; }

    public Boolean[] IsRecurrent { get; set; } 
}

The Boolean array I am currently using is as long as the days of the week and should be accessed using the DaysOfWeek enumerator already provided.

Any ways of solving this?

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
Tich
  • 85
  • 1
  • 6

2 Answers2

0

Array cannot be mapped at all. If you have fixed length you can do some ugly hack like:

internal bool FirstIsRecurrent {
    get { return IsRecurrent[0]; }
    set { IsRecurrent[0] = value; }
}

You will need to provide property for every member of the array and map those properties instead.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
0

Instead of using a Boolean array, you could use an enum.

public class Recurrency
{
    public int Id { get; set; }
    public DateTime BeginDate { get; set; }
    public DateTime EndDate { get; set; }
    public DaysOfWeek IsRecurrent { get; set; }
}

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

In your database DaysOfWeek would be stored as an integer. That integer would map to the true/false for each day of the week.

For example DaysOfWeek == 22, would be (Monday, Tuesday, and Thursday). Or if you thought of it as an array of booleans (false (1), true (2), true (4), false (8), true (16), false (32), false (64))

To set the IsRecurrent you can use the bitwise (|) to combine days.

var recurrency = new Recurrency();
recurrency.IsRecurrent = DaysOfWeek.Monday | DaysOfWeek.Friday;

And then to query the values you could is the HasFlag method.

var recurrency = db.Recurrencies.Find(1);
if (recurrency.IsRecurrent.HasFlag(DaysOfWeek.Monday | DaysOfWeek.Sunday) 
{
    // do something
}
ckal
  • 3,540
  • 1
  • 22
  • 21