如何获取数据表和数据库的强类型linq查询

时间:2021-02-02 00:11:47

I have seen other solution that resemble my problem very closely but somehow it doesn't work for me.

我已经看到了非常接近我的问题的其他解决方案但不知何故它对我不起作用。

I have a linq query to a db with a join to a datatable which is not strongly typed. I need a strongly typed result to return for a view.

我有一个linq查询到一个数据库的连接到一个非强类型的数据表。我需要一个强类型的结果来返回一个视图。

My try looks like this

我的尝试看起来像这样

var query = (from t in queryAllTasks
             join tL in table.AsEnumerable() on t.TaskId equals tL.Field<int>("TaskId")
             select new { t.TaskId, Kasten = tL.Field<int>("Box") }).AsEnumerable();

Here is the datatable if its of interest:

如果感兴趣,这是数据表:

//Create new DataTable.
        DataTable table = new DataTable();

        //Declare DataColumn and DataRow variables
        DataColumn column;
        DataRow row;

        //Create ne DataColumn
        //set DataType
        //ColumnName
        //add to Datatable
        column = new DataColumn();
        column.DataType = System.Type.GetType("System.Int32");
        column.ColumnName = "TaskId";
        table.Columns.Add(column);

        //Dritte Spalte
        column = new DataColumn();
        column.DataType = System.Type.GetType("System.Int32");
        column.ColumnName = "Box";
        table.Columns.Add(column);

1 个解决方案

#1


1  

I suggest to use named custom type, instead of anonymous type. If you are using new { ... } construct, then you are creating anonymous types, which aren't usually passed out or into methods. If you do pass anonymous type out of the method, you need to cast it to object or make formal parameters as dynamic and then access its fields, which I don't really recommend, especially in the view. I suggest to change your query to:

我建议使用命名自定义类型,而不是匿名类型。如果您正在使用新的{...}构造,那么您将创建匿名类型,这些类型通常不会传递给方法或传递给方法。如果您确实从方法中传递了匿名类型,则需要将其转换为对象或将正式参数设置为动态,然后访问其字段,我不建议这样做,尤其是在视图中。我建议将您的查询更改为:

var query = from t in queryAllTasks
             join tL in table.AsEnumerable() on t.TaskId equals tL.Field<int>("TaskId")
             select new MyModel{ TaskId =  t.TaskId, Kasten = tL.Field<int>("Box") };

Where you MyModel is defined as

MyModel定义为的位置

public class MyModel
{
    public int TaskId { get; set; }
    public int Kasten { get; set; }
}

As the result, you will have named IEnumerable<MyModel> in the view with known inner structure, that you can use at your view. Hope this will solve your issue, which as far as I understood, is the lack of ability to access query results from a view.

因此,您将在具有已知内部结构的视图中命名IEnumerable ,您可以在视图中使用它。希望这将解决您的问题,据我所知,该问题是缺乏从视图访问查询结果的能力。

#1


1  

I suggest to use named custom type, instead of anonymous type. If you are using new { ... } construct, then you are creating anonymous types, which aren't usually passed out or into methods. If you do pass anonymous type out of the method, you need to cast it to object or make formal parameters as dynamic and then access its fields, which I don't really recommend, especially in the view. I suggest to change your query to:

我建议使用命名自定义类型,而不是匿名类型。如果您正在使用新的{...}构造,那么您将创建匿名类型,这些类型通常不会传递给方法或传递给方法。如果您确实从方法中传递了匿名类型,则需要将其转换为对象或将正式参数设置为动态,然后访问其字段,我不建议这样做,尤其是在视图中。我建议将您的查询更改为:

var query = from t in queryAllTasks
             join tL in table.AsEnumerable() on t.TaskId equals tL.Field<int>("TaskId")
             select new MyModel{ TaskId =  t.TaskId, Kasten = tL.Field<int>("Box") };

Where you MyModel is defined as

MyModel定义为的位置

public class MyModel
{
    public int TaskId { get; set; }
    public int Kasten { get; set; }
}

As the result, you will have named IEnumerable<MyModel> in the view with known inner structure, that you can use at your view. Hope this will solve your issue, which as far as I understood, is the lack of ability to access query results from a view.

因此,您将在具有已知内部结构的视图中命名IEnumerable ,您可以在视图中使用它。希望这将解决您的问题,据我所知,该问题是缺乏从视图访问查询结果的能力。