LINQ与连接查询只返回一个记录 [英] Linq with join query returning only one record

查看:144
本文介绍了LINQ与连接查询只返回一个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直在努力解决这个问题,在过去的几天中,它确实是令人沮丧的我。我只获得内部显示一条记录 GridView控件

下面是LINQ code我使用的:

 公开IQueryable的GetSubCatWithCatName()
    {
        返回
            (从this.Entities.SubCategory SC
                将C在this.Entities.Category
                在sc.CategoryID等于c.ID                新选择
                {
                    sc.ID,
                    类别名称= c.Name,                    SubCategoryName = sc.Name,
                    SubCategoryImage = sc.ImageURL
                });
    }

我在显示一个 GridView控件组件的结果,不过是越来越显示仅第一条记录

我已经尝试了上述code的其他几个版本,都无济于事。

会有人请帮助?

下面是如何我 GridView控件的定义,如果能有任何帮助。

 < ASP:GridView控件ID =gvSubCategories=服务器的DataKeyNames =ID的AutoGenerateColumns =假
        onrowdatabound =gvSubCategories_RowDataBound
        onselectedindexchanged =gvSubCategories_SelectedIndexChanged>
        <柱体和GT;
            < ASP:CommandField中ShowSelectButton =真/>
            < ASP:的TemplateField>
                <&ItemTemplate中GT;
                    < ASP:图片ID =imgSubCategory=服务器WIDTH =100px的高度=100px的/>
                < / ItemTemplate中>
            < / ASP:的TemplateField>
            < ASP:BoundField的数据字段=类别名称的HeaderText =类别名称/>
            < ASP:BoundField的数据字段=SubCategoryName的HeaderText =子类别名称/>
            < ASP:BoundField的数据字段=SubCategoryImage的HeaderText =子类别图像/>
        < /专栏>
< / ASP:GridView的>

在的.cs类

  gvSubCategories.DataSource =新BASubCategory()GetSubCatWithCatName()。
gvSubCategories.DataBind();

在BASubCategories

 公开IQueryable的GetSubCatWithCatName()
    {
        返回新DASubCategory(this.Entities).GetSubCatWithCatName();
    }

在DASubCategories

 公开IQueryable的GetSubCatWithCatName()
{
    返回
        (从this.Entities.SubCategory SC
            将C在this.Entities.Category
            在sc.CategoryID等于c.ID            新选择
            {
                sc.ID,
                类别名称= c.Name,                SubCategoryName = sc.Name,
                SubCategoryImage = sc.ImageURL
            });
}

在的RowDataBound

 保护无效gvSubCategories_RowDataBound(对象发件人,GridViewRowEventArgs E)
    {
        如果(e.Row.RowType == DataControlRowType.DataRow)
        {
            子类别currentSubCat =(子类别)e.Row.DataItem;            //图片IMG =(图)e.Row.FindControl(imgSubCategory);
            //img.ImageUrl = currentSubCat.ImageURL;
        }
    }


解决方案

基于对你的<一个其它版本的评论href=\"http://stackoverflow.com/questions/20556509/gridview-displaying-only-first-retrieved-record\">question,所述OnRowDatabound处理似乎是有问题的。您的LINQ查询返回一个动态对象。在处理程序,将其转换为类型类别的对象。这将失败 - 虽然我不能解释为什么这仅在第一个记录结束放映。可能与燕子的错误页面级或全局错误处理程序。效果似乎是第一行触发了错误后的GridView停止约束力。

为了解决这个问题,我建议创建一个包含你想从您的LINQ查询,您在GrifView和OnRowDatabound处理程序需要返回属性的类。在处理程序,强制转换为这种类型,并根据需要使用这些数据。

Been trying to solve this issue for the past couple of days and it is really frustrating me. I am only getting a single record displayed inside the GridView.

Below is the LINQ code I am using:

public IQueryable GetSubCatWithCatName()
    {
        return 
            (from sc in this.Entities.SubCategory
                join c in this.Entities.Category
                on sc.CategoryID equals c.ID

                select new
                {
                    sc.ID,
                    CategoryName = c.Name,

                    SubCategoryName = sc.Name,
                    SubCategoryImage = sc.ImageURL
                });
    }

I am displaying the results in a GridView component, however only the first record is getting displayed

I have tried a few other versions of the above code to no avail.

Would anyone please help ?

Below is how my GridView is defined, if it could be of any help.

<asp:GridView ID="gvSubCategories" runat="server" DataKeyNames="ID" AutoGenerateColumns="false"
        onrowdatabound="gvSubCategories_RowDataBound" 
        onselectedindexchanged="gvSubCategories_SelectedIndexChanged">
        <Columns>
            <asp:CommandField ShowSelectButton="true" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Image ID="imgSubCategory" runat="server" Width="100px" Height="100px" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="CategoryName" HeaderText="Category Name" />
            <asp:BoundField DataField="SubCategoryName" HeaderText="Sub Category Name" />
            <asp:BoundField DataField="SubCategoryImage" HeaderText="Sub Category Image" />
        </Columns>
</asp:GridView>

In .cs Class

gvSubCategories.DataSource = new BASubCategory().GetSubCatWithCatName();
gvSubCategories.DataBind();

In BASubCategories

    public IQueryable GetSubCatWithCatName()
    {
        return new DASubCategory(this.Entities).GetSubCatWithCatName();
    }

In DASubCategories

public IQueryable GetSubCatWithCatName()
{
    return 
        (from sc in this.Entities.SubCategory
            join c in this.Entities.Category
            on sc.CategoryID equals c.ID

            select new
            {
                sc.ID,
                CategoryName = c.Name,

                SubCategoryName = sc.Name,
                SubCategoryImage = sc.ImageURL
            });
}

in RowDataBound

protected void gvSubCategories_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            SubCategory currentSubCat = (SubCategory)e.Row.DataItem;

            //Image img = (Image)e.Row.FindControl("imgSubCategory");
            //img.ImageUrl = currentSubCat.ImageURL;
        }
    }

解决方案

Based upon the comments on the other version of your question, the OnRowDatabound handler seems to be problematic. Your Linq query returns a dynamic object. In the handler, you cast it to an object of type Subcategory. This will fail - though I can't explain why this ends in only the first record to be shown. Might be related to a page-level or global error handler that swallows the error. Effect seems to be that the GridView stops binding after the first row has triggered the error.
In order to solve this, I'd suggest to create a class that contains the properties that you want to return from your Linq query and that you need in your GrifView and OnRowDatabound handler. In the handler, cast to this type and use the data as required.

这篇关于LINQ与连接查询只返回一个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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