如何使用LINQ查询中的单个对象填充GridView
本文关键字:单个 对象 填充 GridView 何使用 LINQ 查询 | 更新日期: 2023-09-27 18:25:00
我知道如何用实例集合填充GridView。
var newReviews = from r in myEntities.Reviews
orderby r.Id
select r;
GridView1.DataSource = newReviews.ToList();
GridView1.DataBind();
如何在GridView:中仅检索单个实例
var newReviews = (from r in myEntities.Reviews
orderby r.Id
select r).Single();
....
创建一个包含一个元素的数组:
var newReviews = (from r in myEntities.Reviews
orderby r.Id
select r).Single();
GridView1.DataSource = new[]{ newReviews;}
GridView1.DataBind();
试试这个:
var newReviews = (from r in myEntities.Reviews
orderby r.Id
select r).FirstOrDefault();
if(newReviews != null)
{
var tempList = new List<dynamic>();
tempList.Add(newReviews);
GridView1.DataSource = tempList;
GridView1.DataBind();
}
另一种方法是使用LINQ方法Take()
:
var newReviews = from r in myEntities.Reviews
orderby r.Id
select r;
GridView1.DataSource = newReviews.Take(1).ToList();
GridView1.DataBind();