如何使用 linq/Entity Framework 绑定 gridview?

发布于 2024-11-04 14:33:30 字数 391 浏览 0 评论 0原文

我需要绑定 GridView,我正在使用此代码:

  ProductDBEntities db = new ProductPDBEntities();

    var pro = from u in db.Products where u.PID == 1 select u;

    if (pro != null)
    {
        GridView1.DataSource = pro;
        GridView1.DataBind();
    }

并收到此错误。

系统.InvalidOperationException: 序列包含多个 元素

可以告诉我我做错了什么吗?

I need to bind GridView, I am using this code:

  ProductDBEntities db = new ProductPDBEntities();

    var pro = from u in db.Products where u.PID == 1 select u;

    if (pro != null)
    {
        GridView1.DataSource = pro;
        GridView1.DataBind();
    }

and getting this error.

System.InvalidOperationException:
Sequence contains more than one
element

Can somebody please tell me what am I doin wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

﹂绝世的画 2024-11-11 14:33:30

检查重复,然后尝试绑定它。

我编辑了我的最后一个答案以显示完整的代码:

ProductDBEntities db = new ProductPDBEntities();
GridView1.DataSource = (from u in db.Products where u.PID == 1 select u).First();
GridView1.DataBind();

Check Duplication and then try to bind it.

I have edited my last answer in order to display the complete code :

ProductDBEntities db = new ProductPDBEntities();
GridView1.DataSource = (from u in db.Products where u.PID == 1 select u).First();
GridView1.DataBind();
[浮城] 2024-11-11 14:33:30

此代码可能会有所帮助:

    gv.DataSource = (from u in db.Products .First(u=> u.PID==1)).ToList();

如果您有一张表并且表详细信息可以使用此代码:

    gv.DataSource = (from a in context.tblorder.First(p => p.id == 19).tblorderdetail where a.ID == 19 select a).ToList();

This code may be helpful:

    gv.DataSource = (from u in db.Products .First(u=> u.PID==1)).ToList();

If you have a table and table detail can use this one:

    gv.DataSource = (from a in context.tblorder.First(p => p.id == 19).tblorderdetail where a.ID == 19 select a).ToList();
带上头具痛哭 2024-11-11 14:33:30

首先,您可以检查数据以查看是否有多个 PID = 1 的产品。

其次,您可以使用 .First() 方法来确保只获得一个绑定结果:

var pro = (from u in db.Products where u.PID == 1 select u).First();

First, you might check your data to see if there's more than one product with PID = 1.

Second, you can use the .First() method to make sure you get only one result for binding:

var pro = (from u in db.Products where u.PID == 1 select u).First();
孤君无依 2024-11-11 14:33:30

将变量存储为对象类型并将对象类型作为数据源分配给网格

Store the variable to object type and assign the object type as datasource to grid

穿越时光隧道 2024-11-11 14:33:30

我认为你必须在添加到DataSource时执行ToList()

ProductDBEntities db = new ProductPDBEntities();

var pro = from u in db.Products where u.PID == 1 select u;

if (pro != null)
{
    GridView1.DataSource = pro.ToList();
    GridView1.DataBind();
}

注意:由于它是一个GridView,因此必须采取n个数量行。 无行数限制

I think you have to do ToList() when to add to DataSource:

ProductDBEntities db = new ProductPDBEntities();

var pro = from u in db.Products where u.PID == 1 select u;

if (pro != null)
{
    GridView1.DataSource = pro.ToList();
    GridView1.DataBind();
}

Note: As it is a GridView, has to take n number of rows. No row limit.

仙女 2024-11-11 14:33:30

我这样解决:

ProductDBEntities ctx = new ProductDBEntities ();
var q1 = ctx.Products.Where(x => x.PID == 1).ToList();
GridView1.DataSource = q1;
GridView1.DataBind();

我尝试一下。这是工作。

I solve like this:

ProductDBEntities ctx = new ProductDBEntities ();
var q1 = ctx.Products.Where(x => x.PID == 1).ToList();
GridView1.DataSource = q1;
GridView1.DataBind();

I try it. It's work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文