简体   繁体   English

如何使用LINQ将映射表类组合到一个类中

[英]How to combine Mapping Tables classes into one class using LINQ

Hi I am learning LINQ using C# and need some suggestions how to structure my classes. 嗨,我正在使用C#学习LINQ,并需要一些建议如何构建我的类。

I have 50 existing tables in an sql server I need to create reports from. 我在sql server中有50个现有表,我需要从中创建报表。 These are existing and each have a few years worth of data logging I need to query. 这些是现有的,每个都需要查询几年的数据记录。

I dragged the tables in using the LINQ to SQL designer and get something similar to this: 我使用LINQ to SQL设计器拖动表格并获得类似于此的内容:

[Table(Name = "Data_Log_1")]
public class DataLog1
{
    [Column(Name = "rid", IsPrimaryKey = true)] 
    public int R_Id;

    [Column(Name = "name", CanBeNull = false)]
    public string Name;

    [Column(Name = "Chemical_1_Amount", CanBeNull = false)]
    public int Chemical_1_Amount;

    [Column(Name = "Chemical_2_Amount", CanBeNull = false)]
    public int Chemical_2_Amount;

    // This table continues for another 20 chemicals etc...

}

So on my form I can type query the table and output the result all OK. 所以在我的表单上我可以输入查询表并输出结果全部OK。 It returns an instance of DataLog1 with all my data. 它返回包含所有数据的DataLog1实例。

So I create my second (of 50) table in the same way: 所以我以同样的方式创建我的第二个(50)表:

[Table(Name = "Data_Log_2")]
public class DataLog2
{
    [Column(Name = "rid", IsPrimaryKey = true)] 
    public int R_Id;

    [Column(Name = "name", CanBeNull = false)]
    public string Name;

    [Column(Name = "Chemical_1_Amount", CanBeNull = false)]
    public int Chemical_1_Amount;

    [Column(Name = "Chemical_2_Amount", CanBeNull = false)]
    public int Chemical_2_Amount;

    // This table continues for another 20 chemicals etc...

}

The tables are all identical throughout all 50 data log tables/classes, so I would like to keep them all in one List, or at least be able to pass them as the same type. 这些表在所有50个数据日志表/类中都是相同的,因此我希望将它们保存在一个列表中,或者至少能够将它们作为相同的类型传递。 I cant do this however, because one is a DataLog1, and the other is a DataLog2, even though they are all exactly the same. 我不能这样做,因为一个是DataLog1,另一个是DataLog2,即使它们都完全相同。

Can I create a holder class to do something like this? 我可以创建一个持有者类来做这样的事情吗? So both tables are read into the same type? 那么两个表都被读成相同的类型?

DataLog log1 = from d in dc.DataLog1
                     select d;

DataLog log2 = from d in dc.DataLog2
                     select d;

I would like to be able to add them all to one big list of type DataLog and iterate through them that way if possible. 我希望能够将它们全部添加到DataLog类型的一个大列表中,并在可能的情况下以这种方式迭代它们。 If so, whats the best way of doing this bearing in mind there are 50 tables of around 25 columns each. 如果是这样,最好的方法是记住这里有50个表,每个表约25列。

If im doing something stupid feel free to let me know lol. 如果我做一些愚蠢的事情随时让我知道大声笑。 Thanks for any advice. 谢谢你的建议。

The best option is to try to merge all of the tables into a single table and add a column that differentiates which originating table they came from. 最好的选择是尝试将所有表合并到一个表中,并添加一个列来区分它们来自哪个源表。

If you can't do that, you could create a base class 如果你不能这样做,你可以创建一个基类

public class DataLog
{
    [Column(Name = "rid", IsPrimaryKey = true)] 
    public int R_Id;

    [Column(Name = "name", CanBeNull = false)]
    public string Name;

    [Column(Name = "Chemical_1_Amount", CanBeNull = false)]
    public int Chemical_1_Amount;

    [Column(Name = "Chemical_2_Amount", CanBeNull = false)]
    public int Chemical_2_Amount;
}

and then derive child classes for each table 然后为每个表派生子类

[Table(Name = "Data_Log_1")]
public class DataLog1 : DataLog
{ }

[Table(Name = "Data_Log_2")]
public class DataLog2 : DataLog
{ }

You'd still have to query them all individually, but you could add all the records from each table into a List<DataLog> . 您仍然需要单独查询它们,但您可以将每个表中的所有记录添加到List<DataLog>

Technically, you could create a projection and use Concat to union all your tables into one big projection that you could query against: 从技术上讲,您可以创建一个投影并使用Concat将所有表合并为一个可以查询的大投影:

IQueryable<MyDTO> baseQuery = dc.DataLog1.Select(l => new MyDTO {...})
    .Concat(dc.DataLog2).Select(l => new MyDTO {...})
    .Concat(dc.DataLog3).Select(l => new MyDTO {...})
    ...;

But you'll probably find it easier, and see better performance, if you can use a View to join all of these tables together ahead of time to produce a single LINQ collection that you can query against in the first place. 但是你可能会发现它更容易,并且看到更好的性能,如果你可以使用View提前将所有这些表连接在一起来生成一个可以首先查询的LINQ集合。

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

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