结果LinQ查询到新的DataTable

时间:2012-01-19 05:30:56

标签: c# linq linq-to-sql .net-3.5 .net-4.5

参考之前的帖子/问题: LINQ to Swap few Columns to Rows of a DataTable using C#

我想直接查询新dataTable的查询内容,而不是在新数据表中定义列。

请参阅上面的帖子问题LINQ查询newset

var newSet = dt.AsEnumerable()
           .GroupBy(r => r.Field<string>("Location"))
           .Select(g => new
           {
                Location = g.Key,
                ppl_required_Q1 = g.Where(p => p.Field<string>("Quarter") == "Q1").Sum(p => p.Field<int>("ppl_required")),
                ppl_required_Q2 = g.Where(p => p.Field<string>("Quarter") == "Q2").Sum(p => p.Field<int>("ppl_required")),
                ppl_required_Q3 = g.Where(p => p.Field<string>("Quarter") == "Q3").Sum(p => p.Field<int>("ppl_required")),
                ppl_required_Q4 = g.Where(p => p.Field<string>("Quarter") == "Q4").Sum(p => p.Field<int>("ppl_required")),
                ppl_available_Q1 = g.Where(p => p.Field<string>("Quarter") == "Q1").Sum(p => p.Field<int>("ppl_available")),
                ppl_available_Q2 = g.Where(p => p.Field<string>("Quarter") == "Q2").Sum(p => p.Field<int>("ppl_available")),
                ppl_available_Q3 = g.Where(p => p.Field<string>("Quarter") == "Q3").Sum(p => p.Field<int>("ppl_available")),
                ppl_available_Q4 = g.Where(p => p.Field<string>("Quarter") == "Q4").Sum(p => p.Field<int>("ppl_available")),
            });

如何在没有任何循环的情况下直接在新数据表中获取newset结果集合?是否可以在LINQ ??

1 个答案:

答案 0 :(得分:0)

试试这个:

newset.CopyToDataTable();

Check this Link