将Linq查询存储在列表c#中

时间:2016-08-27 18:07:07

标签: c# linq

我从我的实体框架中有这个类,我想在列表中存储我的linq查询的结果,以便在我的网页表单中显示它,我收到此错误消息:

  

错误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;
        }
    }

1 个答案:

答案 0 :(得分:2)

那里有2个问题。

  1. 首先,您的代码返回匿名类型,但您正在尝试 填充List<Package> - 这就是你得到例外的原因。
  2. 其次,您尝试填充的数据与您拥有的Package类不同,如果您想从函数返回此类列表,则应创建新类型class 拥有所有这些属性。
  3. 所以:

    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>