Return list using select new in LINQ

后端 未结 9 1591
温柔的废话
温柔的废话 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:19

    You cannot return anonymous types from a class... (Well, you can, but you have to cast them to object first and then use reflection at the other side to get the data out again) so you have to create a small class for the data to be contained within.

    class ProjectNameAndId
    {
        public string Name { get; set; }
        public string Id { get; set; }
    }
    

    Then in your LINQ statement:

    select new ProjectNameAndId { Name = pro.ProjectName, Id = pro.ProjectId };
    

提交回复
热议问题