简体   繁体   English

从匿名Linq查询填充WinForms DataGridView

[英]Fill WinForms DataGridView From Anonymous Linq Query

// From my form //来自我的表格

BindingSource bs = new BindingSource();

private void fillStudentGrid()
{
     bs.DataSource = Admin.GetStudents();
     dgViewStudents.DataSource = bs;
}

// From the Admin class //来自Admin类

public static List<Student> GetStudents()

{
    DojoDBDataContext conn = new DojoDBDataContext();

    var query =
        (from s in conn.Students
         select new Student
         {
             ID = s.ID,
             FirstName = s.FirstName,
             LastName = s.LastName,
             Belt = s.Belt
         }).ToList();

    return query;
}

I'm trying to fill a datagridview control in Winforms, and I only want a few of the values. 我正在尝试在Winforms中填充datagridview控件,我只想要一些值。 The code compiles, but throws a runtime error: 代码编译,但会引发运行时错误:

Explicit construction of entity type 'DojoManagement.Student' in query is not allowed. 不允许在查询中显式构造实体类型“DojoManagement.Student”。

Is there a way to get it working in this manner? 有没有办法让它以这种方式工作?

You already have an IEnumerable<Student> instance and you cannot project entities from a query for the reasons described here ) 您已经有一个IEnumerable<Student>实例,由于此处描述的原因,您无法从查询中投影实体)

You also don't need to create a list to bind to this data source - you can greatly simplify your method by changing it to this: 您也不需要创建一个列表来绑定到此数据源 - 您可以通过将其更改为此方法来大大简化您的方法:

public static IEnumerable<Student> GetStudents()
{
    return new DojoDBDataContext().Students;
}

There is no reason to project a new instance to only map a few of the properties, by executing the query you are returning all the values anyhow and your projection doesn't save you anything. 没有理由将新实例投影到仅映射一些属性,通过执行查询无论如何返回所有值,并且您的投影不会保存任何内容。 If you really want to only return a few values from this query for the purposes of information hiding you could do this: 如果您确实只想从信息隐藏中返回此查询中的一些值,则可以执行以下操作:

public static IEnumerable<Object> GetStudents()
{
    DojoDBDataContext conn = new DojoDBDataContext();

    return conn.Students
               .Select(s => new {
                   ID = s.ID,
                   FirstName = s.FirstName,
                   LastName = s.LastName,
                   Belt = s.Belt 
               });
}

Edit: If you aren't using C# 4 you will have to cast the contents of the IEnumerable<T> to Object explicitly. 编辑:如果您不使用C#4,则必须明确地将IEnumerable<T>的内容IEnumerable<T>Object Only C# 4 supports covariance for IEnumerable<T> . 只有C#4支持IEnumerable<T>协方差。 So if you are using C# 3 you will have to do this: 因此,如果您使用的是C#3,则必须执行以下操作:

public static IEnumerable<Object> GetStudents()
{
    DojoDBDataContext conn = new DojoDBDataContext();

    return conn.Students
               .Select(s => new {
                   ID = s.ID,
                   FirstName = s.FirstName,
                   LastName = s.LastName,
                   Belt = s.Belt 
               }).Cast<Object>();
}

Once I was looking around for a solution to display results on a Gridview. 一旦我四处寻找在Gridview上显示结果的解决方案。 The problem I had was that I forgot to set property of datagrid : "Data Source" to None on Winform. 我遇到的问题是我忘了在Winform上将datagrid的属性设置为None:“Data Source”。

This is simple solution for display gridview with linq (just sample code) 这是使用linq显示gridview的简单解决方案(只是示例代码)

DataGrid dataGrid1 = new DataGrid();

var custQuery =
    from cust in db.Customers
    select cust;

dataGrid1.DataSource = custQuery;

Just return a list of objects of an anonymous type : 只需返回匿名类型的对象列表:

public static List<object> GetStudents()
{
    DojoDBDataContext conn = new DojoDBDataContext();

    var query =
        (from s in conn.Students
         select new
         {
             ID = s.ID,
             FirstName = s.FirstName,
             LastName = s.LastName,
             Belt = s.Belt
         }).Cast<object>().ToList();

    return query;
}

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

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