从关系数据库中获取价值

时间:2009-07-07 13:39:26

标签: c# database linq-to-sql

我有一个问题:

我有两个MSSQL表,项和状态,它们通过stateid链接在一起:

    STATES                  ITEMS
-------------    ---------------------------
| id | name |    | id | name | ... | stateid
  V                                     ^
  |_____________________________________|

因此Items.StateID与State.ID相关。在我目前的情况下,我有一个GridView,它被数据绑定到LinqDataSource,它引用Items表。 GridView有两列,一列是Name,另一列是StateID。我希望能够从状态表中提取与StateID相关联的状态的名称,以便显示而不是StateID。

提前致谢!

修改

这是grid / datasource:

<asp:LinqDataSource ID="ItemViewDataSource" runat="server" ContextTypeName="GSFyi.GSFyiDataClassesDataContext"  EnableDelete="true" TableName="FYI_Items" />

<h2 class="gridTitle">All Items</h2>
<telerik:RadGrid ID="ItemViewRadGrid" runat="server" AutoGenerateColumns="False" DataSourceID="ItemViewDataSource" GridLines="None" AllowAutomaticDeletes="True" EnableEmbeddedSkins="False" OnItemDataBound="itemsGrid_ItemDataBound">
<HeaderContextMenu>
    <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
</HeaderContextMenu>
<MasterTableView DataKeyNames="id" DataSourceID="ItemViewDataSource" CommandItemDisplay="None" CssClass="listItems" Width="98%">
    <RowIndicatorColumn>
        <HeaderStyle Width="20px" />
    </RowIndicatorColumn>
    <ExpandCollapseColumn>
        <HeaderStyle Width="20px" />
    </ExpandCollapseColumn>
    <Columns>
        <telerik:GridTemplateColumn ItemStyle-CssClass="gridActions edit" UniqueName="Edit">
            <ItemTemplate>
                <asp:HyperLink ID="edit" runat="server" Text="Edit"></asp:HyperLink>
            </ItemTemplate>
<ItemStyle CssClass="gridActions edit"></ItemStyle>
        </telerik:GridTemplateColumn>
        <telerik:GridButtonColumn ConfirmText="Are you sure you want to delete this item?" ConfirmDialogType="RadWindow" ButtonType="LinkButton" ItemStyle-CssClass="gridActions delete"  CommandName="Delete" >
<ItemStyle CssClass="gridActions delete"></ItemStyle>
        </telerik:GridButtonColumn>
        <telerik:GridBoundColumn DataField="name" HeaderText="Item Name" SortExpression="name"
            UniqueName="name">
        </telerik:GridBoundColumn>
                    <telerik:GridTemplateColumn HeaderText="State" UniqueName="state">
                        <ItemTemplate>
                            <asp:Label ID="stateLbl" runat="server" Text='<%# Eval("stateid") %>' />
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
    </Columns>
</telerik:RadGrid>

目前的代码隐藏:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Cms.Web.UI;
using Telerik.Web.UI;

public partial class Custom_Modules_GSFyi_Backend_Views_ItemsListView : ViewModeUserControl<ItemsView>
{
    protected void Page_Load(object sender, EventArgs e)
    {
        addNewItem.NavigateUrl = CreateHostViewCommand("ItemsInsertView",null,null);
    }
    protected void itemsGrid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
        {
            var item = (GSFyi.FYI_Item)e.Item.DataItem;
            HyperLink edit = (HyperLink)e.Item.FindControl("edit");
            edit.NavigateUrl = CreateHostViewCommand("ItemsEditView", item.id.ToString(), null);

        }
    }
}

这有帮助吗?

3 个答案:

答案 0 :(得分:4)

假设您已在LINQtoSQL数据类中定义了关系,并且LINQDataSource控件指向您的Items实体,您应该能够在GridView标记中直接使用以下数据绑定语法:

<asp:GridView ID="ItemsGridView" runat="server" DataKeyNames="id" DataSourceID="ItemsLinqDataSource">
    <Columns>
        <asp:TemplateField>
                    <ItemTemplate>
                            <%# Eval("states.name") %>
                    </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

<asp:LinqDataSource ID="ItemsLinqDataSource" runat="server" ContextTypeName="DataClassesDataContext" TableName="items"></asp:LinqDataSource>

答案 1 :(得分:1)

尝试这样的事情:

var query = from s in States
            join i in Items on s.ID equals i.StateID
            select i.Name;

LINQ to SQL提供程序将接受此查询并将其转换为SQL查询,如下所示:

select i.Name
from Items i
    inner join States s on i.StateID = s.ID;
  

建议阅读 SQL INNER JOIN Keyword

答案 2 :(得分:1)

试一试:

var query = from i in Items
            join s in States on s.id equals i.stateid
            select s.name, i.name