2

I have a Collection of Data and the Dictionary:

  • Collection handle Student Data
  • Dictionary Keeps Student Courses.

I want to get the Course Name from the Dictionary and put it into as CourseName.

private viod GenerateStudentDetails(Student studentData) { var courses = m_courses.GetCoursesDictionary(); // returns Dictionary<Guid,string>() var studentDetails= from data in studentData select new { FirstName = data.FirstName, LastName = data.LastName, Email = data.Email, Mobile = data.Profile.Mobile, City = data.Profile.City, PostCode = data.Profile.PostCode, CourseName = courses[data.Profile.CourseID ?? Guid.Empty] }; } 

"LINQ to Entities does not recognize the method 'System.String get_Item(System.Guid)' method, and this method cannot be translated into a store expression."

2 Answers 2

3

You could try something as the below:

private viod GenerateStudentDetails(Student studentData) { var courses = m_courses.GetCoursesDictionary(); // returns Dictionary<Guid,string>() var studentDetails= (from data in studentData select new { FirstName = data.FirstName, LastName = data.LastName, Email = data.Email, Mobile = data.Profile.Mobile, City = data.Profile.City, PostCode = data.Profile.PostCode, CourseID = data.Profile.CourseID }).AsEnumerable() .Select(item=>new { FirstName = item.FirstName, LastName = item.LastName, Email = item.Email, Mobile = item.Profile.Mobile, City = item.Profile.City, PostCode = item.Profile.PostCode, CourseName = courses[item.Profile.CourseID ?? Guid.Empty] }); } 

What's the problem?

The problem is that the last expression in the anonymous type you create,

 CourseName = courses[data.Profile.CourseID ?? Guid.Empty] 

cannot be in this place, because it can't be translated appropriately. So you have this option. You can declare a sequence of the data you want from the studentData and then make any conversion of call anything you want to the new anonymous type we create.

Sign up to request clarification or add additional context in comments.

5 Comments

It would be nice if you could add a bit of explanation why calling AsEnumerable before accessing the dictionary helps.
@MarcinJuraszek I think now it's more clear. You caught me on writing this :)
AsEnumerable() because  System.Collections.Generic.Dictionary<TKey, TValue> does not inherit from IEnumerable Interface
@Christos, Thanks for your solution. Now it's working
@Iresh - You can thank by accepting the answer. Check this - meta.stackexchange.com/questions/5234/…
0

Just for thought

var eCourses = ((IEnumerable<KeyValuePair<Guid, string>>) courses); var studentDetails = (from data in studentData select new { data.FirstName, data.LastName, data.Email, data.Profile.Mobile, data.Profile.City, data.Profile.PostCode, CourseName = eCourses.FirstOrDefault(s => s.Key == data.Profile.CourseID).Value }); 

1 Comment

I tried it but exception throws "Unable to create a constant value of type 'System.Collections.Generic.KeyValuePair`2'. Only primitive types or enumeration types are supported in this context."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.