所以我有一个DataSet,我将其视为IEnumerable,然后应用一个LINQ表达式,将模数组收集到var g中。这是代码隐藏...
protected void Page_Load(object sender, EventArgs e)
{
LinqSamples samples = new LinqSamples(gvNumbers, this.Page);
samples.DataSetLinq40();
}
private class LinqSamples
{
private DataSet testDS;
private GridView _gv;
private Page _pg;
public LinqSamples(GridView gv, Page pg)
{
testDS = TestHelper.CreateTestDataSet(pg);
_gv = gv;
_pg = pg;
}
public void DataSetLinq40()
{
var numbers = testDS.Tables["Numbers"].AsEnumerable();
var numberGroups = from n in numbers
group n by n.Field<int>("number") % 5 into g
select new { Remainder = g.Key, Numbers = g };
_gv.DataSource = numberGroups;
_gv.DataBind();
}
}
internal static DataSet CreateTestDataSet(Page pg)
{
DataSet ds = new DataSet();
// Customers Table
ds.Tables.Add(CreateNumbersTable());
ds.AcceptChanges();
return ds;
}
private static DataTable CreateNumbersTable()
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
DataTable table = new DataTable("Numbers");
table.Columns.Add("number", typeof(int));
foreach (int n in numbers)
{
table.Rows.Add(new object[] { n });
}
return table;
}
...和.aspx页面......
<asp:GridView ID="gvNumbers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="Group" DataField="Remainder" />
<asp:BoundField HeaderText="Number" DataField="Numbers" />
</Columns>
</asp:GridView>
我想要的是GridView中的一列具有模数组,例如0,另一列具有该组中的所有数字,逗号分隔,因此0表示“0, 5“,1表示”1,6“,表示2”2,7“等。如何使用我已经拥有的linq结果,包含数据?
当我运行页面时,第二列显示g的类型为System.Collections.Generic.List1[System.Data.DataRow]
答案 0 :(得分:1)
您可能正在协助查询数据源而不是集合,请使用.ToList()
_gv.DataSource = justNums.ToList();
_gv.DataBind();