0

谁能向我解释一下这个错误是什么以及为什么我得到它?我试图更改f变量的名称,但它没有用。

public ActionResult Index(string SortOrder, string CurrentFilter, string SearchString, int? page)
{

    ViewBag.CurrentSort = SortOrder;
    ViewBag.NameSortParm = string.IsNullOrEmpty(SortOrder) ? "Tillverkning desc" : "";
    ViewBag.DateSortParm = SortOrder == "HireDate" ? "HireDatedesc" : "HireDate";

    conn.Open();

    ProductionOrderList myList = new  ProductionOrderList();
    myList = ProductionOrderReg.GetProductionOrders(conn, new Guid("288937bcc-6uif-485a-anm9-fadfq1234039d"));

    if (!string.IsNullOrEmpty(SearchString))
    {     
        // here on myList get red line     
        myList = myList.Where( myList => myList .ProductionOrderNo.ToUpper().Contains(SearchString.ToUpper()));
    }
}
4

2 回答 2

4

这是因为 C# 支持闭包。编译器抱怨是因为 lambda 参数myList的名称必须与外部范围内的任何变量的名称不同。

尝试这个:

myList = myList.Where(x => x.ProductionOrderNo.ToUpper().Contains(SearchString.ToUpper()));

请参阅Lambda 表达式(C# 编程指南)


您遇到的下一个问题myList是类型为ProductionOrderListwhile Whereretuns an IEnumerable<T>. 除非您需要使用自定义集合类型,否则我不会使用它。尝试这个:

 IEnumerable<tionOrderItem> myList = ProductionOrderReg.GetProductionOrders(...);
 if (!string.IsNullOrEmpty(SearchString))
 {     
      // here on myList get red line     
      myList = myList.Where(x => x .ProductionOrderNo.ToUpper().Contains(SearchString.ToUpper()));
 }

 ...

Or, you really must use an instance of ProductionOrderList, make sure you have a constructor that takes an IEnumerable<T>, and use this:

 ProductionOrderList myList = ProductionOrderReg.GetProductionOrders(...);
 if (!string.IsNullOrEmpty(SearchString))
 {     
      // here on myList get red line     
      myList = new ProductionOrderList(
          myList.Where(x => x .ProductionOrderNo.ToUpper().Contains(SearchString.ToUpper())));
 }
于 2013-05-12T17:57:08.507 回答
1

Change your code to

myList = myList.Where( item => item .ProductionOrderNo.ToUpper().Contains(SearchString.ToUpper()));
于 2013-05-12T17:58:04.660 回答