2013-06-24 48 views
1

引用嵌套lambda表達式參數出於某種原因,我得到在Microsoft Visual Studio 2012的錯誤在下面的代碼:錯誤在LINQ ASP.NET

students.Where(s => 
    foreignStudents.Any(f => 
     s.FirstName == f.FirstName && 
     s.LastName == f.LastName 
    ) 
); 

student爲學生提供各種名單屬性包括FirstNameLastNameforeignStudents是僅包含學生的FirstNameLastName的列表。我已經更改了變量名稱,以便更容易理解問題。

它說IEnumerable不包含'Any'的定義,並且最佳擴展方法重載Enumerable.Any<TSource>(IEnumerable<TSource>, Func<TSource,bool>)有一些無效參數。

將其切換到f => truef => f.FirstName == "Sarah"可消除該錯誤。

任何有識之士將不勝感激!


編輯:實際代碼

// Retreives orders from the database using parameters from the URL 
string orderQuery = "SELECT * FROM EventOrders WHERE EventID = @0 AND AccountNum = @1"; 
var orders = db.Query(orderQuery, Request.Form["id"], Request.Form["accountnum"]); 

// Parses order IDs from the URL 
// Where Order IDs are in the form: <orderNum>-<orderLine>[,...] 
var orderIDs = Request.QueryString["orderids"].Split(',') 
    .Select(orderID => { 
     var components = orderID.Split('-'); 
     return new { 
      OrderNum = components[0].AsInt(), 
      OrderLine = components[1].AsInt() 
     }; 
    }); 

var quantityList = orders 
    .Where(o => orderIDs.Any(i => o.OrderNum == i.OrderNum && o.OrderLine ==    i.OrderLine)) 
    .OrderByDescending(o => o.Quantity) 
    .Select(o => new { o.OrderNum, o.OrderLine, o.Quantity }) 

編輯2: 所以我覺得它可能只是與Visual Studio的一個問題了。在調試剩下的代碼後,它似乎工作。儘管Visual Studio仍然以紅色表示錯誤。

+1

實際代碼請... –

+0

嘗試在條件附近添加括號。 –

+0

當我們有一個[SSCCE](http://www.sscce.org/)複製,粘貼並查看我們自己的bug時,它可以幫助我們 –

回答

0

你真正想要在這裏做的是Join。通過加入這兩個表格,您可以在一個表格中找到存在於另一個表格中的所有學生。這也是一種可以更有效地完成的操作,它描述了您在其中搜索每個項目的整個數據集的內容。如果知道你正在做一個Join它可以進行非常嚴格的優化。

var query = from student in students 
      join foreignStudent in foreignStudents 
       on new { student.FirstName, student.LastName } 
       equals new { foreignStudent.FirstName, foreignStudent.LastName } 
       into joinedStudents 
      where joinedStudents.Count() > 0 
      select student;