如何使用动态列查询数据表

发布于 2024-10-21 03:13:29 字数 212 浏览 0 评论 0原文

我需要查询包含未知列的数据表以返回列的子集。

显然,使用数据视图这很容易,但是 LINQ 怎么样?

我将把数据表和列名作为参数传递给应该执行查询的方法。

我没有 LINQ 经验,到目前为止我所看到的 SO 看起来 LINQ 的唯一动态部分是 WHERE 过滤器,而不是选择哪些列。

我错了吗?

如果可以的话你能给我提供一个样品吗?

I need to query a datatable with unknown columns to return a subset of columns.

Obviously this is easy with a dataview, but how about LINQ?

I will be passing a datatable and column names as parameters to the method which should do the query.

I have NO LINQ experience and what I've seen thus far on SO makes it seem that the only dyynamic part of LINQ would be the WHERE filter, and NOT which columns get selected.

Am I wrong?

If so could you provide me with a sample?

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

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

发布评论

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

评论(2

匿名。 2024-10-28 03:13:29

以下是如何使用 Linq to DataSet 选择任意两列的快速示例:

public static void SelectRandomColumns(DataTable dataTable, string col1, string col2)
{
    // Select into an anonymous type here, but you could
    // select into a custom class
    var query = from row in dataTable.AsEnumerable()
                select new
                {
                    p1 = row[col1],
                    p2 = row[col2]
                };

    // Do whatever you need to do with the new collection of items
    foreach (var item in query)
    {
        Console.WriteLine("{0}: {1}, {2}: {3}", col1, item.p1, col2, item.p2);
    }           
}

如果您需要选择随机数量的列,那么这会变得有点棘手,但希望这能让您朝着正确的方向前进。

Here is a quick sample of how you could select any two columns using Linq to DataSet:

public static void SelectRandomColumns(DataTable dataTable, string col1, string col2)
{
    // Select into an anonymous type here, but you could
    // select into a custom class
    var query = from row in dataTable.AsEnumerable()
                select new
                {
                    p1 = row[col1],
                    p2 = row[col2]
                };

    // Do whatever you need to do with the new collection of items
    foreach (var item in query)
    {
        Console.WriteLine("{0}: {1}, {2}: {3}", col1, item.p1, col2, item.p2);
    }           
}

If you need to select a random number of columns, then this would get a little trickier, but hopefully this will get you headed in the right direction.

薄情伤 2024-10-28 03:13:29

我没有使用过它,但是 .NET 3.5 / VS2008 附带的动态 LINQ 库允许您拥有基于字符串的选择子句。这是 链接到 Scott Guthrie 的博客(其中还包含下载该库的链接)。

Scott 博客中的图像显示了一个示例动态 LINQ 表达式,它表明表达式的每个主子句都可以是生成的字符串。

在此处输入图像描述

希望这有帮助!

I have not used it, but the Dynamic LINQ library that came with .NET 3.5 / VS2008 allows you to have string-based select clauses. Here's a link to Scott Guthrie's blog about it (which also includes links to download the library).

An image from Scott's blog shows a sample Dynamic LINQ expression, and it shows that each main clause of the expression can be a generated string.

enter image description here

Hope this helps!

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