简体   繁体   English

LINQ ASP.NET中嵌套lambda表达式中的参数引用错误

[英]Error in referencing parameters in nested lambda expression in LINQ ASP.NET

For some reason I'm getting an error in Microsoft Visual Studio 2012 over the following code: 由于某些原因,我在Microsoft Visual Studio 2012中通过以下代码收到错误:

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

student is a list of students with various attributes including FirstName and LastName and foreignStudents is a list containing only the FirstName and LastName of students. student是具有各种属性的学生的列表,包括FirstNameLastNameforeignStudents是仅包含学生的FirstNameLastName的列表。 I've changed the variable names to make it easier to understand the problem. 我更改了变量名称,以使其更容易理解问题。

It says that IEnumerable does not contain a definition for 'Any' and the best extension method overload Enumerable.Any<TSource>(IEnumerable<TSource>, Func<TSource,bool>) has some invalid arguments. 它说IEnumerable不包含'Any'的定义,并且最佳扩展方法重载Enumerable.Any<TSource>(IEnumerable<TSource>, Func<TSource,bool>)有一些无效的参数。

Switching it to f => true or f => f.FirstName == "Sarah" removes the error. 将其切换为f => truef => f.FirstName == "Sarah"消除该错误。

Any insight would be much appreciated! 任何见解将不胜感激!


Edit: Actual Code 编辑:实际代码

// 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 })

Edit 2: So I think it might just be a problem with Visual Studio now. 编辑2:所以我认为现在Visual Studio可能只是一个问题。 It seems work after debugging the rest of the code. 调试其余代码后,似乎可以正常工作。 Visual Studio still has it underlined in red indicating an error though. Visual Studio仍然用红色下划线标明了错误。

What you're really looking to do here is a Join . 您真正要在这里做的是Join By joining the two tables you can find all of the students in one table that exist in the other. 通过将两个表连接起来,您可以在一个表中找到另一个表中所有的学生。 This is also an operation that can be done much more efficiently that what you're describing in which you search the entire set of data for each item . 与您在其中搜索每个项目的整个数据集所描述的内容相比,此操作可以更有效地完成。 If it's known that you're doing a Join it can be very heavily optimized. 如果知道您正在执行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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM