我正在深入研究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();
我在这里做错了什么?
由于
答案 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();