C# 如何管理具有相同结构的多个表

C# 如何管理具有相同结构的多个表,c#,visual-studio,linq,linq-to-sql,C#,Visual Studio,Linq,Linq To Sql,我有几个表具有相同的结构(例如Queue1、Queue2等)。由于行政原因,我不得不把它们分开。某些队列表可能由应用程序创建 我使用的是数据上下文和linq和C 现在我想对所有表执行一个操作。我用这种方式检索我的桌子 IEnumerable<MetaTable> tables = appelsDataContext.Mapping.GetTables(); foreach (MetaTable table in tables) {

我有几个表具有相同的结构(例如Queue1、Queue2等)。由于行政原因,我不得不把它们分开。某些队列表可能由应用程序创建

我使用的是数据上下文和linq和C

现在我想对所有表执行一个操作。我用这种方式检索我的桌子

        IEnumerable<MetaTable> tables = appelsDataContext.Mapping.GetTables();
        foreach (MetaTable table in tables)
        {
        }
IEnumerable tables=appelsDataContext.Mapping.GetTables();
foreach(表中的元表)
{
}
简单地说,我想在所有这些表中更新一个userID字段。我不知道如何做到这一点,因为从我的dataContext角度来看,即使表具有相同的结构,它们都是不同的实体

非常感谢您能提供的任何帮助。非常感谢!
Mathieu

如果您在解决方案资源管理器中的dbml文件下查看DataClasses1.designer.cs-file(或您命名的任何文件),您将发现类似这样的类

[global::System.Data.Linq.Mapping.TableAttribute(Name="Production.Product")]
public partial class Product : INotifyPropertyChanging, INotifyPropertyChanged
{
//...
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ProductID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int ProductID
{
    get
    {
        return this._ProductID;
    }
    set
    {
        if ((this._ProductID != value))
        {
            this.OnProductIDChanging(value);
            this.SendPropertyChanging();
            this._ProductID = value;
            this.SendPropertyChanged("ProductID");
            this.OnProductIDChanged();
        }
    }
}
//...
}
等等

如果您创建了一个接口

public interface ITableHasProductId
{
    int ProductID {get; set;}
}
您可以将该接口应用于所有表,将以下代码放在单独的文件中(以避免在重新生成数据上下文时丢失更改)


然后,您可以将所有表放入一个
IEnumerable
中,并更新ProductID。

Awsome!非常感谢你的帮助!不过有一点细节。您的代码在“public int ProductID{get;set;}”上给出了一个错误,表示“public”修饰符在那里是错误的。但是,如果ProductID表字段不是公共的,我如何访问它呢?又来了@Mathieu,我更新了我的答案,界面成员上不应该有公共修饰符,它们是隐式公共的。我只是觉得我应该发布并说,虽然这是一个好主意,但如果你坚持使用VB,它是不起作用的。这是由于VBs关于显式实现接口的愚蠢规则造成的:(
public partial class Product : ITableHasProductId {}