Geeks With Blogs
Keep It Simple Simple Solutions for Complex Scenarios

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

Posted on Sunday, October 11, 2009 11:18 PM | Back to top


Comments on this post: To my wish list for .Net 4.0: Linq to Entities

# re: To my wish list for .Net 4.0: Linq to Entities
Requesting Gravatar...
Thanks for the helpful post...I was facing the same error while coding for my website, I searched a lot for the particular error a lot.. I came across your site and all my doubts got cleared...Thanks a lot again...
emphysema remedy
Left by Gterisa Galathe on May 11, 2011 7:39 AM

# re: To my wish list for .Net 4.0: Linq to Entities
Requesting Gravatar...
Thanks for sharing superb information. Your website is so cool. I am impressed by the info that you have on this site. It reveals how nicely you understand this subject. Bookmarked this website page, will come back for more articles.
skin tag removal
Left by wilford cimo on Jun 01, 2011 12:03 AM

# re: To my wish list for .Net 4.0: Linq to Entities
Requesting Gravatar...
Excellent post. I simply stumbled upon your blog and wanted to say that I have genuinely loved reading your blog posts. Any ways I shall be subscribing for your feed and I hope you post again soon.
bulk playing cards
Left by melwin jason on Jun 02, 2011 7:26 AM

# re: To my wish list for .Net 4.0: Linq to Entities
Requesting Gravatar...
This site is really very interesting..I like this site ..It is very informative..pillow to stop snoring
Left by roy luton on Jun 03, 2011 2:47 AM

# re: To my wish list for .Net 4.0: Linq to Entities
Requesting Gravatar...
the helpful post...I was facing the same error while coding for my website, I searched a lot for the particular error a lot.. I came across your site and all my doubts got cleared...get a free iphone
Left by Pakayaa on Jun 18, 2011 4:53 AM

# re: To my wish list for .Net 4.0: Linq to Entities
Requesting Gravatar...
This is such a excellent article to read. Stimulating me to read more of your articles. Carry on the nice work. Hoping to see more excellent posts from you shortly.
snoring products
Left by maxie mcaferty on Jun 29, 2011 2:04 AM

# re: To my wish list for .Net 4.0: Linq to Entities
Requesting Gravatar...
You did make some interesting points over there. Thanks.
seo company
Left by percy thomppson on Jul 20, 2011 5:21 AM

# great
Requesting Gravatar...
I am very enjoyed for this blog. Its an informative topic.It help me very much to solve some problems. Its opportunity are so fantastic and working style so speedy.I think it may be help all of you.Thanks a lot for enjoying this beauty blog with me. I am appreciating it very much! Looking forward to another great blog. Good luck to the author! all the best!

mma fight gear
Left by mma fight gear on Sep 02, 2011 4:15 AM

# re: To my wish list for .Net 4.0: Linq to Entities
Requesting Gravatar...
Issue in the behavior of Linq to Entities hope now resolved.freelancer
Left by freelancer on Sep 24, 2011 2:25 AM

# re: To my wish list for .Net 4.0: Linq to Entities
Requesting Gravatar...
Dot net 4.0 is full of features i liked it very much. Keep posting such updates buddy. natural mole removal
Left by Mak Creg on Oct 17, 2011 7:57 AM

Your comment:
 (will show your gravatar)


Copyright © Juan Mestas J. (aka gotchas) | Powered by: GeeksWithBlogs.net