继my original question之后,成功得到了答案。
当我包含Where()方法时,为什么以下代码有效且更通用的示例失败?
// *** WORKS ***
private IEnumerable<int> _MySelectedData;
public void method()
{
IEnumerable<int> distinctData = GetData(m => m.ModelID);
}
public IEnumerable<int> GetData(Func<Model, int> selectParam)
{
return _MyData
.Where(m => _MySelectedData.Contains(m.ManufacturerID))
.Select(selectParam).Distinct();
}
// *** FAILS ***
private IEnumerable<int> _MySelectedManufacturers;
public void method()
{
IEnumerable<int> distinctData = GetData(_MySelectedManufacturers, m => m.ManufacturerID, m => m.ModelID);
}
public IEnumerable<int> GetData(IEnumerable<int> _MySelectedData, Func<Model, int> whereParam, Func<Model, int> selectParam)
{
return _MyData
.Where(m => _MySelectedData.Contains(whereParam))
.Select(selectParam).Distinct();
}
我收到以下错误:
&#39; IEnumerable的&#39;不包含&#39;包含&#39;的定义和最好的扩展方法重载&#39; Queryable.Contains ...
此行发生错误:
.Where(m => _MySelectedData.Contains(whereParam))
希望有人可以用我的方式强调错误!
答案 0 :(得分:2)
Contains
将值作为参数,但您传递的是函数。我怀疑你只想根据“当前”项评估函数:
.Where(m => _MySelectedData.Contains(whereParam(m)))