引用嵌套lambda表達式參數出於某種原因,我得到在Microsoft Visual Studio 2012的錯誤在下面的代碼:錯誤在LINQ ASP.NET
students.Where(s =>
foreignStudents.Any(f =>
s.FirstName == f.FirstName &&
s.LastName == f.LastName
)
);
student
爲學生提供各種名單屬性包括FirstName
和LastName
和foreignStudents
是僅包含學生的FirstName
和LastName
的列表。我已經更改了變量名稱,以便更容易理解問題。
它說IEnumerable不包含'Any'的定義,並且最佳擴展方法重載Enumerable.Any<TSource>(IEnumerable<TSource>, Func<TSource,bool>)
有一些無效參數。
將其切換到f => true
或f => 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仍然以紅色表示錯誤。
實際代碼請... –
嘗試在條件附近添加括號。 –
當我們有一個[SSCCE](http://www.sscce.org/)複製,粘貼並查看我們自己的bug時,它可以幫助我們 –