适用于所有表的 LINQ 2 SQL 方法

发布于 2024-09-10 08:55:07 字数 700 浏览 6 评论 0原文

我的标题可能略有偏差,但这就是我正在尝试做的事情。我有一个 L2S 方法,适用于我想编写一次的每个表。这是为了设置一个软锁定列,我还需要一个 Read 和 UnLock 方法。这是我到目前为止所做的:

public static void LockRow(string TableName, int TablePrimaryKey)
    {
        using (var context = McpDataContext.Create())
        {
            var tableToLock = (from lockTable in context.tblPlans
                               where lockTable.PlanID == TablePrimaryKey
                               select lockTable).Single();

            tableToLock.Locked = true;
            context.SubmitChanges();
        }
    }

我想做的是将 context.tblPlans 替换为 context.TableName。这在 LINQ 中可能吗?为何如此?我假设我的处理方式是错误的,所以我将不胜感激一些方向/指示。

谢谢

My Title may be slightly off but here is what I am trying to do. I have a L2S Method that would be for every table that I would like to write once. This is to set a soft lock column where I will also need a Read and UnLock method. Here is what I have so far:

public static void LockRow(string TableName, int TablePrimaryKey)
    {
        using (var context = McpDataContext.Create())
        {
            var tableToLock = (from lockTable in context.tblPlans
                               where lockTable.PlanID == TablePrimaryKey
                               select lockTable).Single();

            tableToLock.Locked = true;
            context.SubmitChanges();
        }
    }

What I would like to do is replace context.tblPlans with context.TableName. Is this possible in LINQ? How so? I am assumming that I am going about it the wrong way so I'd be grateful for some direction/pointers.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猛虎独行 2024-09-17 08:55:07

更新,因为第一个示例不起作用。

您可以使用通用方法和接口来完成此操作:

public interface IPlanTable
{
    int PlanID { get; set; }
}

public static void LockRow<TEntity>(int TablePrimaryKey) where TEntity : class, IPlanTable
{
    using (var context = McpDataContext.Create())
    {
        var tableToLock = (from lockTable in context.GetTable<TEntity>()
                           where lockTable.PlanID == TablePrimaryKey
                           select lockTable).Single();

        tableToLock.Locked = true;
        context.SubmitChanges();
     }
}

您还必须利用 Linw2SQL 表被创建为部分类的事实来扩展它们,以便所有相关表都实现 IPlanTable

您将使用它如下所示:

LockRow<tblPlan>(23); 

只需将 tblPlan 替换为表类的名称即可。

然而,这不允许您在运行时设置表,LinqToSQL 是面向对象且类型安全的,指定要检索的表与其设计的工作方式相反。

Update becuase the first example would not work.

You could do it with a generic method and an interface:

public interface IPlanTable
{
    int PlanID { get; set; }
}

public static void LockRow<TEntity>(int TablePrimaryKey) where TEntity : class, IPlanTable
{
    using (var context = McpDataContext.Create())
    {
        var tableToLock = (from lockTable in context.GetTable<TEntity>()
                           where lockTable.PlanID == TablePrimaryKey
                           select lockTable).Single();

        tableToLock.Locked = true;
        context.SubmitChanges();
     }
}

You will also have to use the fact that the Linw2SQL tables are created as partial classes to extend them so all the relevent table implement IPlanTable

You would use it like below:

LockRow<tblPlan>(23); 

simply replace tblPlan with whatever the name of your table class is.

However this won't allow you to set the table at runtime, LinqToSQL is object orientated and type safe, specifying the table you want to retreive is contrary to how it si designed to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文