Iresh
Iresh

Reputation: 91

How to get value from Dictionary to new Object in LINQ query

I have a Collection of Data and the Dictionary:

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."

Upvotes: 2

Views: 951

Answers (2)

Sodoshi
Sodoshi

Reputation: 141

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
                        });

Upvotes: 0

Christos
Christos

Reputation: 53958

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.

Upvotes: 3

Related Questions