Return list using select new in LINQ

后端 未结 9 1592
温柔的废话
温柔的废话 2020-11-29 01:59

This is my method which gives me error.

public List GetProjectForCombo()
{
    using (MyDataContext db = new MyDataContext (DBHelper.GetConnect         


        
9条回答
  •  北海茫月
    2020-11-29 02:21

    Method can not return anonymous type. It has to be same as the type defined in method return type. Check the signature of GetProjectForCombo and see what return type you have specified.

    Create a class ProjectInfo with required properties and then in new expression create object of ProjectInfo type.

    class ProjectInfo
    {
       public string Name {get; set; }
       public long Id {get; set; }
    }
    
    public List GetProjectForCombo()
    {
        using (MyDataContext db = new MyDataContext (DBHelper.GetConnectionString()))
        {
            var query = from pro in db.Projects
                        select new ProjectInfo(){ Name = pro.ProjectName, Id = pro.ProjectId };
    
            return query.ToList();
        }
    }
    

提交回复
热议问题