使用LINQ读取对象 [英] reading an object using LINQ

查看:56
本文介绍了使用LINQ读取对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我准备了一组对象,这些对象旨在将数据作为对象包含在内,并且,我试图通过ASP.NET来弄清楚如何使用LINQ来检索数据对象的相对分层组织。



我的第一次尝试使用一个读取ContactDetails对象的var,但是,我意识到我不能在页面之外使用这个var来编写代码,而我想通过asp.net控件检索数据。



任何人都可以指导我并给我一个例子,因为我觉得有点迷失了吗?



I have prepared a set of objects, which are meant to contain data as objects, and, I am trying to figure out how to use LINQ to retrieve a relatively hierarchical organization of data objects, through ASP.NET.

My first attempt uses a var which reads a ContactDetails object, however, I realize that I cannot use this var outside of the page where I can write code, whilst I would like to retrieve the data through the asp.net controls.

Can anyone guide me and provide me with an example, because I feel a bit lost?

CustomerManagement.aspx.cs
protected void Page_Load(object sender, EventArgs e)
    {
        var query = from ContactDetail contactdetail in ContactDetail.ContactDetails
                    select contactdetail;
    }





CustomerManagement.aspx



CustomerManagement.aspx

<ajaxToolkit:TabPanel ID="TabPanel2"  runat="server" 

                HeaderText="Customer Details">
                <HeaderTemplate>
                    Customer Details
                </HeaderTemplate>
                <ContentTemplate><br />Name: 
                    <asp:TextBox ID="txtName" runat="server" style="margin-left: 18px" 

                        Width="175px">John</asp:TextBox>
                    Surname:
                    <asp:TextBox ID="txtSurname" runat="server" Width="154px">Doe</asp:TextBox>
                    <br />
                    Address:
                    <asp:TextBox ID="txtAddressLine1" runat="server" Width="406px">123 Maverick Heights</asp:TextBox>
                    <asp:TextBox ID="txtAddressLine2" runat="server" Width="470px"></asp:TextBox>
                    <asp:TextBox ID="txtAddressLine3" runat="server" Width="470px">Arkansas</asp:TextBox>
                    <br />
                    <br />
                    Subscription Start: 01/01/2013 Subscription Expiry: 
                    31/12/2016<br />





我注意到文本框控件似乎没有数据源,所以我希望有更好的控件可用于查询面向对象的数据模型,否则我将需要打开与Jet Engine数据库的连接,但是,我们的导师不鼓励我们使用结构化和面向对象的编程,所以我坚持在对象中使用数据,因为当Microsoft Office是一个类似的模型似乎已被采用设计。



I have noticed that the textbox control does not seem to have a data source, so I am hoping a better control is available for use to query an object oriented data model, otherwise I will require opening a connection to a Jet Engine database, however, our tutor discouraged us from using both structured and object oriented programming so I am sticking to the use of data within objects, as a similar model seems to have been adopted when Microsoft Office was designed.

推荐答案

您可以使用任何数据列表控件(如Repeater,DetailView等)将Linq对象结果绑定到它的数据源。它为您提供了更简单的方法来定义布局/设计以显示数据,而不仅仅是使用标签或文字。



或者您仍然可以使用如下所示的查询在任何地方使用该对象分配给显示控件



You can use any of the Data listing control such as Repeater , DetailView etc. to bind the Linq object results to the Datasource of it. It gives you much more easy way to define the layout/design to display data rather than just using label or literals.

Or still you can use the query like below and use the object anywhere to assign to the display controls

(from ContactDetail contactdetail in ContactDetail.ContactDetails
                            select new ModelClass
                            {
                                ModelClassProperty1 = contactdetail.Property1,
                                ModelClassProperty2 = contactdetail.Property2,
                            }).FirstOrDefault();



//应该将属性类名称更改为刚刚给出的用于理解的属性。


//Should change the property class names as its just given for understanding.


这篇关于使用LINQ读取对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆