简体   繁体   中英

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

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM