2

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

这是代码:

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();

        }
     }
4

1 回答 1

1

您需要一个函数来接收您要显示的 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();
}
于 2012-10-29T21:01:36.030 回答