【问题标题】:How can I merge results using entity framework and bind the results to gridview如何使用实体框架合并结果并将结果绑定到 gridview
【发布时间】:2013-02-02 15:35:33
【问题描述】:

大家好,我已经编写了以下查询来获取基于技术 ID 的信息,并且我得到了正确的结果,但是我不知道如何根据我的要求合并结果

循环将根据加载结果的网格进行处理,当有 2 行时,此循环将运行多次,然后我将在每个循环上获得结果,我想将这些记录绑定在一起结果网格,就像我们合并数据集一样我想合并结果

foreach (GridViewRow row in grdForum.Rows)
{
int quesID = Convert.ToInt16(grdForum.DataKeys[row.RowIndex].Values["TechID"].ToString());
var lastpost = db.tblQuestions.Where(u => u.TechID.Equals(quesID)).OrderByDescending(u => u.DatePosted).Take(1).ToList();
grdnewUser.DataSource = lastpost.ToList();
grdnewUser.DataBind();
}

 public class Results
    {
        public DateTime DatePosted;
        public string QuestionTitle;
        public string UserName;
    }

public void bindLastPost()
{
   ArrayList a = new ArrayList();
   foreach (GridViewRow row in grdForum.Rows)
   {
    int quesID = Convert.ToInt16(grdForum.DataKeys[row.RowIndex].Values["TechID"].ToString());
    var lastpost = db.tblQuestions.Where(u => u.TechID.Equals(quesID)).OrderByDescending(u => u.DatePosted).Take(1).FirstOrDefault();

    a.Add(new Results { DatePosted = lastpost.DatePosted, QuestionTitle = lastpost.QuestionTitle, UserName = lastpost.UserName });
   }
      grdnewUser.DataSource = a;
      grdnewUser.DataBind();

根据user1974729的回答

int[] quesID;
            int i = 0;
            foreach (GridViewRow row in grdForum.Rows)
            {
                quesID[i] = Convert.ToInt16(grdForum.DataKeys[row.RowIndex].Values["TechID"].ToString());
                i++;
            }
            //Check this part to find out how u will implement the Contains in Linq
            var lastpost = from e in db.tblQuestions where quesID.Contains(e.TechID) select e;

            }

【问题讨论】:

  • 不确定我是否正确理解了您的问题,但您不能只使用 AddRange 方法吗?
  • 您想将从数据库中获取的结果与gridview中已经存在的结果合并吗?
  • 嗨,检查我的编辑 daryal 和 niaher

标签: c# asp.net entity-framework


【解决方案1】:
u r doing this inside the foreach loop//
No matter how many times u do this , only the last time u do it will stick..
grdnewUser.DataSource = lastpost.ToList(); //This is done only one time

所以我猜你正在寻找的是如何在 Linq 中实现 CONTAINS ..

int[] quesID;
int i = 0;
foreach (GridViewRow row in grdForum.Rows)
{
 quesID[i] = Convert.ToInt16(grdForum.DataKeys[row.RowIndex].Values["TechID"].ToString());
 i++;
}

//Check this part to find out how u will implement the Contains in Linq
var lastpost = db.tblQuestions.Where(u => quesID.Contains(quesID)).OrderByDescending(u => u.DatePosted).Take(1).ToList();
grdnewUser.DataSource = lastpost.ToList();
grdnewUser.DataBind();
}

【讨论】:

  • 嗨错误'int[]' does not contain a definition for 'Contains' and the best extension method overload 'System.Linq.ParallelEnumerable.Contains<TSource>(System.Linq.ParallelQuery<TSource>, TSource)' has some invalid arguments
  • 这就是为什么我说 //检查这部分以了解你将如何在 Linq 中实现 Contains .. 如果这就是你的样子 4..
  • blogs.msdn.com/b/alexj/archive/2009/03/26/… .... 如果你想写一个 IN(quesid1,quesid2,quesid3,quesid4) ,那就是链接 2 参考
  • 您好,检查我的编辑,我收到一个错误Use of unassigned local variable 'quesID'
  • 不知道你必须在循环中放置一个断点来检查 quesID 是否正在初始化。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多