LINQ与链式方法 - 上下文错误

时间:2013-07-10 23:26:14

标签: c# asp.net-mvc-3 linq

我正在深入研究LINQ,VS2012在OrderBy中对我的LINQ语句非常贴切:

The name 'x' does not exist in the current context

我想要做的是:

SELECT * FROM Contacts 
WHERE DateCreated >= [SOMEDATE VALUE] 
AND   DateCreated <= [SOMEDATE VALUE]
ORDER BY DateCreated DESC
LIMIT 100

LINQ

model.Contacts = db.Contacts.Where((x =>(x.DateCreated >= p.DateFrom) && (x.DateCreated <= p.DateTo))).OrderByDescending(x.DateCreated).Take(100).ToList();

我在这里做错了什么?

由于

1 个答案:

答案 0 :(得分:3)

你的lambda表达式中有太多括号。此外,您在OrderByDescending方法调用中没有lambda变量。

试试这个:

model.Contacts = db.Contacts
    .Where(x => x.DateCreated >= p.DateFrom && x.DateCreated <= p.DateTo)
    .OrderByDescending(x => x.DateCreated).Take(100).ToList();