简体   繁体   English

返回列表使用 select new in LINQ

[英]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 ?你能告诉我如何只返回ProjectIdProjectNam吗?

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.检查 GetProjectForCombo 的签名并查看您指定的返回类型。

Create a class ProjectInfo with required properties and then in new expression create object of ProjectInfo type.创建具有所需属性的 class ProjectInfo,然后在新表达式中创建 ProjectInfo 类型的 object。

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();
    }
}
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>();
   }

} }

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 返回匿名类型......(嗯,你可以,但你必须先将它们转换为 object 然后在另一端使用反射再次获取数据)所以你必须为创建一个小的 class要包含在其中的数据。

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

Then in your LINQ statement:然后在您的 LINQ 声明中:

select new ProjectNameAndId { Name = pro.ProjectName, Id = pro.ProjectId };
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>();
    }
}

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返回的是匿名类型,因此创建一个包含 2 个字段的新 class

class BasicProjectInfo {
   string name;
   string id;
}

and return new BasicProjectInfo(pro.ProjectName, pro.ProjectId);并返回new BasicProjectInfo(pro.ProjectName, pro.ProjectId); . . You method in this case will return a List<BasicProjectInfo>在这种情况下,您的方法将返回一个List<BasicProjectInfo>

Your method's return value has to be a List<Project> .您的方法的返回值必须是List<Project>

Using select new you are creating an instance of an anonymous type, instead of a Project .使用select new您正在创建匿名类型的实例,而不是Project

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>();
    }
}

You're creating a new type of object therefore it's anonymous.您正在创建一种新类型 object,因此它是匿名的。 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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM