gpt4 book ai didi

c# - 无法在 asp mvc 中隐式转换类型 'System.Collections.Generic.List

转载 作者:行者123 更新时间:2023-11-30 23:18:52 25 4
gpt4 key购买 nike

我需要使用 Linq 加入 4 个表格。但它告诉我这个错误:

Error 1 Cannot implicitly convert type 'System.Collections.Generic.List<>' to 'System.Collections.Generic.List' E:\MyProject\GoKhoda\GoKhoda\Areas\Classes\StudentsFunc.cs 20 20 GoKhoda

这是我的代码:

public List<Tbl_Students> ShowAllStudents()
{
var qstudent = from s in _db.Tbl_Students
join pr in _db.Tbl_Pye_Reshte on s.StudentID equals pr.StudentID
join r in _db.Tbl_Reshte on pr.ReshteID equals r.ReshteID
join p in _db.Tbl_Paye on pr.PayeID equals p.PayeID
orderby p.PayeID descending
select new { StudentName = s.StudentName, StudentFamily = s.StudentFamily, StudentImage = s.StudentImage, StudentPayeName = p.PayeName, StudentReshtName = r.ReshteName };
return qstudent.ToList();
}

为什么要显示错误?我该如何解决这个问题?

最佳答案

使用这个:

var qstudent = from s in _db.Tbl_Students
join pr in _db.Tbl_Pye_Reshte on s.StudentID equals pr.StudentID
join r in _db.Tbl_Reshte on pr.ReshteID equals r.ReshteID
join p in _db.Tbl_Paye on pr.PayeID equals p.PayeID
orderby p.PayeID descending
select new Tbl_Students { StudentName = s.StudentName, StudentFamily = s.StudentFamily, StudentImage = s.StudentImage, StudentPayeName = p.PayeName, StudentReshtName = r.ReshteName };
return qstudent.ToList();

Select new { ... }只会创建一个匿名类型,它不能转换为任何 除了object .

顺便说一句:您应该考虑根据应用程序中的实体来命名您的类型,在您的示例中为 Student ,而不是存储实体的表。

关于c# - 无法在 asp mvc 中隐式转换类型 'System.Collections.Generic.List,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40574360/

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