下面的代码正在工作.. CustomView 被填充而没有任何错误。
自定义视图
public static ProductView CustomView(Product data)
{
var view = new ProductView();
view.ID = data.ID;
view.Number = data.Number;
var invetoryCount = GetInvetoryCountNumber(data.Number);
if (invetoryCount < 1)
view.SoldOut = true;
return view;
}
功能
public static List<ProductView> GetAll()
{
using (var ctx = new DBSolutionEntities())
{
var data = ctx.Products.OrderBy(p => p.Name).ToList();
return data.Select(CustomView).ToList();
}
}
我想在函数中添加一个额外的参数来跳过库存搜索
CustomView - 添加了布尔值
public static ProductView CustomView(Product data, bool skipInvCheck)
{
var view = new ProductView();
view.ID = data.ID;
view.Number = data.Number;
if(skipInvCheck)
{
var invetoryCount = GetInvetoryCountNumber(data.Number);
if (invetoryCount < 1)
view.SoldOut = true;
}
return view;
}
当我将 bool 参数添加到 CustomView 并调用它时
public static List<ProductView> GetWithoutInvSoldOut()
{
using (var ctx = new DBSolutionEntities())
{
var data = ctx.Products.OrderBy(p => p.Name).ToList();
return data.Select(CustomView, false).ToList();
}
}
我收到以下错误:
无法从用法中推断方法“System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable, System.Func)”的类型参数。尝试明确指定类型参数。
传递参数时如何调用返回填充视图返回 data.Select(CustomView, false).ToList()?
ps 当获取 ONE 记录(不是 .ToList )时,CustomView 通过 return CustomView(data, false) 工作;