C# 如何向gridview添加新查询?

C# 如何向gridview添加新查询?,c#,C#,我有一个数据列表,我想使用该数据在表中搜索。查询工作正常,但不保留以前的数据。有什么解决方案吗 代码如下: foreach (string Id in LstID) { GdEmp.DataSource = employee.ShowData(Id); GdEmp.DataBind(); } 问题是: public class Employee { public string family { get; set; } public string name {

我有一个数据列表,我想使用该数据在表中搜索。查询工作正常,但不保留以前的数据。有什么解决方案吗

代码如下:

foreach (string Id in LstID)
{
    GdEmp.DataSource = employee.ShowData(Id);
    GdEmp.DataBind();

}
问题是:

public class Employee
{
    public string family { get; set; }
    public string name { get; set; }
    ....


public List<Employee> ShowData(string Id)
    {
        try
        {
            var Query = from P in Bank.employee
                where P.Id == Id
                select new Employee
                {
                    family = P.Family,
                    name= P.Name,
                    ...
                };
            return Query.ToList();

        }
     }
公共类员工
{
公共字符串族{get;set;}
公共字符串名称{get;set;}
....
公共列表ShowData(字符串Id)
{
尝试
{
var Query=来自Bank.employee中的P
其中P.Id==Id
选择新员工
{
家庭,
名称=P.名称,
...
};
返回Query.ToList();
}
}

您需要一个函数,该函数接收要显示的Id列表,并返回包含相应信息的列表,而不是逐个获取它们

GdEmp.DataSource = employee.ShowAllData(LstID);
GdEmp.DataBind();
使用此功能:

public List<Employee> ShowAllData(List<string> LstID)
{
        var q = from P in Bank.employee
                where LstID.Contains(P.Id)
                select new Employee
                {
                       family = P.Family,
                       name   = P.Name,
                       ...
               };
        return q.ToList();
}
public List ShowAllData(List LstID)
{
var q=来自Bank.employee中的P
其中LstID.包含(P.Id)
选择新员工
{
家庭,
名称=P.名称,
...
};
返回q.ToList();
}

在foreach循环的每次迭代中,您都会覆盖GdEmp的数据源。这只会保留上一次迭代的数据。请尝试让我们更好地了解您要完成的任务,我们可能会帮助您。该代码只会在列表中显示上一次
id
的数据。我知道,但我想这样做保留以前的数据。我怎么做?你说它不保留以前的数据是什么意思?你发布的代码中以前的数据是什么?需要更多信息吗?我总是得到最后一个查询。但我想保留所有查询