将LinqQuery存储在一个列表c#中

本文关键字:一个 列表 LinqQuery 存储 | 更新日期: 2023-09-27 18:04:13

我的实体框架中有这个类,我想将linq查询的结果存储在列表中,以在我的web表单中显示它,我收到了以下错误消息:

错误4无法隐式转换类型"System.Collections.Generic.List"到"System.Collections.Generic.List"''App_Code''Models''PackageModel.cs

这是我的代码:

谢谢你的帮助。

public partial class Package
{
    public int ID { get; set; }
    public int SchoolID { get; set; }
    public int SessionID { get; set; }
    public int LessonsID { get; set; }
    public virtual Lesson Lesson { get; set; }
    public virtual School School { get; set; }
    public virtual Session Session { get; set; }
}
public List<Package> GetAllItemPackage()
{
    try
    {
        using (MusicSchoolEntities db = new MusicSchoolEntities())
        {
            List<Package> packages = (from x in db.Packages                                           
                                      join y in db.Schools on x.SchoolID equals y.ID
                                      join z in db.Sessions on x.SessionID equals z.ID
                                      join w in db.Lessons on x.LessonsID equals w.ID
                                      join q in db.Courses on w.CourseID equals q.ID
                                      select new { SchoolTitle = y.TitleEn , 
                                                   SessionName = z.NameEn, 
                                                   CourseTitle = q.TitleEn, 
                                                   LessonPeriod = w.PeriodEn, 
                                                   LessonPrice = w.PriceEn, 
                                                   LessonDesc = w.DescriptionEn, 
                                                   LessonPaymtd = w.PayMtdEn}).ToList();
                return packages;
            }
        }
        catch (Exception)
        {
            return null;
        }
    }

将LinqQuery存储在一个列表c#中

您有两个问题。

  1. 首先,您的代码返回一个匿名类型,但您正在尝试以填充List<Package>-这就是您获得异常的原因
  2. 其次,您试图填充的数据与您所拥有的Package类不同,因此,如果您想从函数返回这样的列表,您应该创建一个新类型的类以拥有所有这些属性

因此:

List<YourClass> packages = (from x in db.Packages                                           
                          join y in db.Schools on x.SchoolID equals y.ID
                          join z in db.Sessions on x.SessionID equals z.ID
                          join w in db.Lessons on x.LessonsID equals w.ID
                          join q in db.Courses on w.CourseID equals q.ID
                          select new YourClass
                          {
                              SchoolTitle = y.TitleEn , 
                              SessionName = z.NameEn, 
                              CourseTitle = q.TitleEn, 
                              LessonPeriod = w.PeriodEn, 
                              LessonPrice = w.PriceEn, 
                              LessonDesc = w.DescriptionEn, 
                              LessonPaymtd = w.PayMtdEn
                          }).ToList();
return packages;

当然GetAllItemPackage应该返回:List<YourClass>而不是List<Package>