IEnumerable为Out参数

时间:2013-11-17 10:14:04

标签: c# asp.net linq entity-framework-4

我正在尝试编写一个方法,该方法可以提供前10条记录以及总数。记录,从表中。无法弄清楚如何访问输出参数。 它给出了一个错误 - 使用泛型类型IEnumerable需要1个类型的参数。 如果有更好的方法,请建议。

DAL:

public void GetData(out IEnumerable<tblCountry> iList, out int rowCount)
{    
  iList = DBContext.tblCountries.Where(c=>c.Id > 0).Take(10);    
  rowCount = DBContext.tblCountries.Where(c=>c.Id > 0).Count();    
}

BLL:

public void GetData(out IEnumerable<tblCountry> iList, out int rowCount)
{    
  objDAL.GetData(out iList, out rowCount);    
}

代码背后:

objBLL.(out IEnumerable<tblCountry> iList, out int rowCount);//Error here

1 个答案:

答案 0 :(得分:2)

在您的代码中,您应首先声明2个变量,然后将它们作为out参数传递给GetData方法:

IEnumerable<tblCountry> iList;
int rowCount;
objBLL.GetData(out iList, out rowCount);

作为替代方法,您可以设计一个模型来表示此操作的结果,并让该方法返回此模型:

public class MyModel
{
    public IEnumerable<tblCountry> Countries { get; set; }
    public int RowCount { get; set; }
}

然后:

public MyModel GetData()
{
    MyModel model = new MyModel();
    model.Countries = DBContext.tblCountries.Where(c=>c.Id > 0).Take(10);
    model.RowCount = DBContext.tblCountries.Where(c=>c.Id > 0).Count();
    return model;
}

然后像这样调用它:

MyModel model = objBLL.GetData();
// here you could use model.Countries and model.RowCount