0

In my application I am accessing my data using Entity FrameWork. Its a WPF MVVM application. I want my Entity Data to be changed into an ObservableCollection. Is there a way to do this? Help me out. Thanks in advance.

Satish Nissankala
  • 141
  • 1
  • 3
  • 12

1 Answers1

0

The most flexible way for you to split your tiers/concerns is for you write a converter for each type. This is generally known as converting an Entity Data Type (EDO) to a Data Transfer Type (DTO) and vice-versa. Here's a potential example:

    public ObservableCollection<DTO.Schedule> GetSchedules(DateTime day)
    {
        using (var ctx = new MyContext())
        {
            var endOfDay = day.Date.Add(new TimeSpan(23, 59, 59));
            var found = from schedule in ctx.Schedules
                where (schedule.Date >= day.Date) && (schedule.Date <= endOfDay)
                select schedule;

            return new ObservableCollection<DTO.Schedule>(found.Select(GetSchedule));
        }
    }

    private static DTO.Schedule GetSchedule(EDO.Schedule schedule)
    {
        return schedule == null
            ? null
            : new DTO.Schedule
            {
                Id = schedule.ScheduleID,
                Name = schedule.Name,
                Description = schedule.Description,
                Status = schedule.Status,
                Date = schedule.Date,
            };
    }
Jesse C. Slicer
  • 19,901
  • 3
  • 68
  • 87