linq,选择列作为 IEnumerable

发布于 2024-08-29 04:31:24 字数 482 浏览 4 评论 0原文

我该如何在 linq 中执行此操作:

IEnumerable<DataRow> query = 
        from rec in dt.AsEnumerable() 
        where rec.Field<decimal>("column2") == 1 && foo(rec.Field<decimal>("column1"))
        select new {
            column1 = rec.Field<decimal>("column1"),
            column2 = rec.Field<decimal>("column2"),
            column3 = rec.Field<decimal>("column3")}  ;

这不起作用。我尝试选择一些列作为新的数据表,然后将其与其他一些数据表连接起来。

how can i do in linq:

IEnumerable<DataRow> query = 
        from rec in dt.AsEnumerable() 
        where rec.Field<decimal>("column2") == 1 && foo(rec.Field<decimal>("column1"))
        select new {
            column1 = rec.Field<decimal>("column1"),
            column2 = rec.Field<decimal>("column2"),
            column3 = rec.Field<decimal>("column3")}  ;

this does not work. Im trying to select some columns as new datatable then join it later with some other datatable.

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

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

发布评论

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

评论(1

红焚 2024-09-05 04:31:24

您可以使用 http://code.msdn.microsoft.com/LinqEntityDataReader
然后选择您的值,如下所示:

DataTable query = 
        (from rec in dt.AsEnumerable() 
        where rec.Field<decimal>("column2") == 1 && foo(rec.Field<decimal>("column1"))
        select new {
            column1 = rec.Field<decimal>("column1"),
            column2 = rec.Field<decimal>("column2"),
            column3 = rec.Field<decimal>("column3")}).ToDataTable();

一种更简单的解决方案(不需要外部库)是创建一个 DataTable(如果您还没有),并按如下方式填充它:

// declaring a data table.. Replace it with whatever code you want
var table = new DataTable();
table.Columns.Add("column1", typeof(Decimal));
table.Columns.Add("column2", typeof(Decimal));
table.Columns.Add("column3", typeof(Decimal));

// Populate the table
(from rec in dt.AsEnumerable() 
where rec.Field<decimal>("column2") == 1 && foo(rec.Field<decimal>("column1"))
select new {
    column1 = rec.Field<decimal>("column1"),
    column2 = rec.Field<decimal>("column2"),
    column3 = rec.Field<decimal>("column3")})
    .Aggregate(table, (dt, r) => { dt.Rows.Add(r.column1, r.column2, r.column3); return dt; });

// at this point your table variable is populated

You can use http://code.msdn.microsoft.com/LinqEntityDataReader
and then select your values like this:

DataTable query = 
        (from rec in dt.AsEnumerable() 
        where rec.Field<decimal>("column2") == 1 && foo(rec.Field<decimal>("column1"))
        select new {
            column1 = rec.Field<decimal>("column1"),
            column2 = rec.Field<decimal>("column2"),
            column3 = rec.Field<decimal>("column3")}).ToDataTable();

A much more simple solution, that doesn't require external libraries, would be to create a DataTable (if you don't already have one), and populate it as follows:

// declaring a data table.. Replace it with whatever code you want
var table = new DataTable();
table.Columns.Add("column1", typeof(Decimal));
table.Columns.Add("column2", typeof(Decimal));
table.Columns.Add("column3", typeof(Decimal));

// Populate the table
(from rec in dt.AsEnumerable() 
where rec.Field<decimal>("column2") == 1 && foo(rec.Field<decimal>("column1"))
select new {
    column1 = rec.Field<decimal>("column1"),
    column2 = rec.Field<decimal>("column2"),
    column3 = rec.Field<decimal>("column3")})
    .Aggregate(table, (dt, r) => { dt.Rows.Add(r.column1, r.column2, r.column3); return dt; });

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