使用字段<>使用 Linq 到 SQL

发布于 2024-11-29 06:52:48 字数 621 浏览 1 评论 0 原文

我正在尝试将列名传递给类并从 SQL 数据库检索该列的值。看起来像 Field<>不适用于表,但适用于数据表。有人能指出我如何使用 linq 做到这一点吗?我确信这是一件非常简单的事情。谢谢。

[Table(Name = "[Keys]")]
public class Keys
{
    [Column]
    public string Column_A{ get; set; }
    [Column]
    public string Column_B{ get; set; }
    [Column]
    public string Column_C{ get; set; }
}

    public string ReadKey(string DBKey)
{
    DataContext dc = new DataContext(sqlconn);
    Table<Keys> keysTable = dc.GetTable<Keys>();

    var query = from k in keysTable.AsEnumerable()
    select k.Field<string>("DBKey");     <---------------- wrong

}

I'm trying to pass a column name to a class and retrieve a value for that column from a sql database. Looks like Field<> is not available for a Table, but it will work with DataTable. Can someone point me how can i do it using linq? I'm sure it is a very simple thing. Thanks.

[Table(Name = "[Keys]")]
public class Keys
{
    [Column]
    public string Column_A{ get; set; }
    [Column]
    public string Column_B{ get; set; }
    [Column]
    public string Column_C{ get; set; }
}

    public string ReadKey(string DBKey)
{
    DataContext dc = new DataContext(sqlconn);
    Table<Keys> keysTable = dc.GetTable<Keys>();

    var query = from k in keysTable.AsEnumerable()
    select k.Field<string>("DBKey");     <---------------- wrong

}

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

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

发布评论

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

评论(2

樱花坊 2024-12-06 06:52:48

Field<> 用于 LINQ to DataTables,并作为 System.Data 的扩展包含在内。它不是标准的 LINQ 扩展方法。请参阅此问题,了解如何使用 LINQ to DataTables

您想要做的事情可以通过使用表达式来完成,或者 动态 LINQ。动态 LINQ 不是 .NET 附带的一部分;它作为示例包含在 VS2008 中,并且人们一直在继续使用它。我不确定微软是否支持、更新等。在将其纳入框架之前我不会指望它。

您可以创建一个方法,创建 Jon 的答案中提到的 selector ,如下所示:

public Expression<Func<Keys,T>> GetSelectLambda<T>(string propertyName)
{
    ParameterExpression lhsParam = Expression.Parameter(typeof(Keys), "s");
    Expression fieldParam = Expression.Property(lhsParam, propertyName);
    var theExpression = Expression.Lambda<Func<Keys, T>>(fieldParam, lhsParam);
    return theExpression;
}

因此传入 GetSelectLambda("DBKey") 应生成 s 的 lambda => s.DBKey,然后返回并传递到 Jon 的方法中。

希望这有帮助。

The Field<> is for LINQ to DataTables, and is included as an extension to System.Data. It's not a standard LINQ extension method. See this SO question on how to use LINQ to DataTables.

What you're trying to do can be accomplished by using an Expression, or Dynamic LINQ. Dynamic LINQ is not part of what's shipped with .NET; it was included as an example in VS2008 and people have continued to use it. I'm not sure if it's supported, updated, etc. by Microsoft. I wouldn't count on it until it's incorporated into the framework.

You could create a method create the selector mentioned in Jon's answer as such:

public Expression<Func<Keys,T>> GetSelectLambda<T>(string propertyName)
{
    ParameterExpression lhsParam = Expression.Parameter(typeof(Keys), "s");
    Expression fieldParam = Expression.Property(lhsParam, propertyName);
    var theExpression = Expression.Lambda<Func<Keys, T>>(fieldParam, lhsParam);
    return theExpression;
}

So passing in GetSelectLambda<string>("DBKey") should generate a lambda of s => s.DBKey, which is then returned and passed into Jon's method.

Hope this helps.

彩扇题诗 2024-12-06 06:52:48

通常不应在 LINQ to SQL 中使用列name - 与使用非类型化 DataTable 对象相比,LINQ to SQL 的好处之一是可以获得更高的编译时安全性。

如果您需要允许选择不同的列,则可以使用表达式树来选择它们:

public string ReadKey(Expression<Func<Keys, string>> selector)
{
    DataContext dc = new DataContext(sqlconn);
    Table<Keys> keysTable = dc.GetTable<Keys>();

    // Generally speaking you don't want to use AsEnumerable here...
    var query = keysTable.Select(selector);
    // Now do something with query
}

请注意,使用GetTable很少是一个好主意 - 通常您会工作具有特定 DataContext 上的特定表。

You generally shouldn't use a column name with LINQ to SQL - one of the benefit of LINQ to SQL over using untyped DataTable objects is that you get more compile-time safety.

If you need to allow different columns to be selected, you can use an expression tree to select them:

public string ReadKey(Expression<Func<Keys, string>> selector)
{
    DataContext dc = new DataContext(sqlconn);
    Table<Keys> keysTable = dc.GetTable<Keys>();

    // Generally speaking you don't want to use AsEnumerable here...
    var query = keysTable.Select(selector);
    // Now do something with query
}

Note that using GetTable is rarely a great idea - normally you'd work with a specific table on a specific DataContext.

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