Return list using select new in LINQ

This is my method which gives me error.

public List<Project> GetProjectForCombo() { using (MyDataContext db = new MyDataContext (DBHelper.GetConnectionString())) { var query = from pro in db.Projects select new { pro.ProjectName, pro.ProjectId }; return query.ToList(); } } 

If i change it with this:

public List<Project> GetProjectForCombo() { using (MyDataContext db = new MyDataContext (DBHelper.GetConnectionString())) { var query = from pro in db.Projects select pro; return query.ToList(); } } 

Then it works fine with no errors.

Can you please let me know that how I can return only ProjectId and ProjectNam?

2

9 Answers

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<ProjectInfo> 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(); } } 
2
public List<Object> GetProjectForCombo() { using (MyDataContext db = new MyDataContext (DBHelper.GetConnectionString())) { var query = db.Project .Select<IEnumerable<something>,ProjectInfo>(p=> return new ProjectInfo{Name=p.ProjectName, Id=p.ProjectId); return query.ToList<Object>(); } 

}

1

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 }; 
3
public List<Object> GetProjectForCombo() { using (MyDataContext db = new MyDataContext (DBHelper.GetConnectionString())) { var query = from pro in db.Projects select new {pro.ProjectName,pro.ProjectId}; return query.ToList<Object>(); } } 
1

try this solution for me its working

 public List<ProjectInfo> GetProjectForCombo() { using (MyDataContext db = new MyDataContext (DBHelper.GetConnectionString())) { return (from pro in db.Projects select new { query }.query).ToList(); } } 

What is being returned is an anonymous type so create a new class with 2 fields

class BasicProjectInfo { string name; string id; } 

and return new BasicProjectInfo(pro.ProjectName, pro.ProjectId);. You method in this case will return a List<BasicProjectInfo>

1

Your method's return value has to be a List<Project>.

Using select new you are creating an instance of an anonymous type, instead of a Project.

4

You can do it as following:

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

You're creating a new type of object therefore it's anonymous. You can return a dynamic.

public dynamic GetProjectForCombo() { using (MyDataContext db = new MyDataContext (DBHelper.GetConnectionString())) { var query = from pro in db.Projects select new { pro.ProjectName, pro.ProjectId }; return query.ToList(); } } 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like