1

出于某种原因,我在 Microsoft Visual Studio 2012 中通过以下代码收到错误:

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

student是具有各种属性的学生列表,包括FirstNameandLastNameforeignStudents是仅包含学生的FirstName和的列表LastName。我更改了变量名称,以便更容易理解问题。

它说 IEnumerable 不包含“任何”的定义,并且最好的扩展方法重载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 仍然用红色下划线表示错误。

4

1 回答 1

0

What you're really looking to do here is a 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.

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;
于 2013-06-24T20:30:35.540 回答