I am working with Entity Framework as model in a REST service, using the CollectionService<T> and i found a little issue in the behavior of Linq to Entities,this is well know but i hope a change in .Net 4.0
Currently Linq to Entities not supports projections that returns types with parameterized constructors, by instance for a method that return a IEnumerable<KeyValuePair<string, Course>> i wanna make
1: .Select(c => new KeyValuePair<string, CustomType>(c.StringProperty, c))
This projection for me is pretty natural, but thrown the next error: “Only parameterless constructors and initializers are supported in LINQ to Entities”
My workaround (model is an instance of my Entity Framework ObjectContext)
1: protected override IEnumerable<KeyValuePair<string, Course>> OnGetItems()
2: {
3:
4: List<Course> Course = model.Courses.Select(c => new Course() { Code = c.Code, Description = c.Description, Name = c.Name, OpenDate = c.OpenDate }).ToList();
5: Dictionary<string, Course> returnCourse = new Dictionary<string, Course>();
6: Course.ForEach((Course item) =>
7: {
8: returnCourse.Add(item.Code, item);
9: });
10:
11: return returnCourse;
12:
13:
14: }
See you