简体   繁体   English

尝试从Linq绑定GridView,不确定如何提取我需要的数据

[英]Trying to bind GridView from Linq, not sure how to extract data I need

So I have a DataSet I'm treating as an IEnumerable, and then applying a LINQ expression which gathers modulo groups into a var g. 所以我有一个数据集,我将其视为IEnumerable,然后应用LINQ表达式,该表达式将取模组收集到var g中。 Here is the code-behind... 这是隐藏的代码...

    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;
        }

...and the .aspx page... ...以及.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>

What I want is for one column in the GridView to have the modulo group, say for example 0, and the other column to have all the numbers in that group, comma delimited, so for 0 that would be, "0, 5", for 1 that would be "1, 6", for 2 "2, 7", etc. How can I do this with the linq result I already have, which contains the data? 我想要的是让GridView中的一列具有取模组,例如0,而另一列具有该组中的所有数字,以逗号分隔,因此对于0来说应该是“ 0、5”, 1代表“ 1,6”,2代表“ 2,7”,等等。如何使用已有的linq结果(包含数据)执行此操作?

When I run the page the second column show that g is of type System.Collections.Generic.List1[System.Data.DataRow] 当我运行页面时,第二列显示g为System.Collections.Generic.List1[System.Data.DataRow]类型

You are probably assinging query to datasource instead of collection, use .ToList() 您可能正在将查询赋值给数据源而不是集合,请使用.ToList()

_gv.DataSource = justNums.ToList();
_gv.DataBind();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM