gpt4 book ai didi

c# - 如何在 LINQ 查询中从字典获取值到新对象

转载 作者:行者123 更新时间:2023-11-30 21:53:38 25 4
gpt4 key购买 nike

我有一个数据集和字典:

  • 收集处理学生数据
  • 词典保留学生类(class)。

我想从字典中获取类(class)名称并将其作为类(class)名称放入。

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

最佳答案

你可以试试下面的方法:

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?

问题是你创建的匿名类型中的最后一个表达式,

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

不能在这个地方,因为翻译不当。所以你有这个选择。您可以从 studentData 声明您想要的数据序列,然后将任何您想要的调用转换为我们创建的新匿名类型。

关于c# - 如何在 LINQ 查询中从字典获取值到新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33750564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com