我有这段代码
private List<Autocomplete> _GetPeople(string query)
{
List<Autocomplete> people = new List<Autocomplete>();
var results = (from p in db.People
where (p.FirstName + " " + p.LastName).Contains(query)
orderby p.FirstName, p.LastName
select p).Take(10).ToList();
}
我想知道如何用这种格式编写等效的东西
private ApplicationDbContext _dbContext;
public PeopleController()
{
_dbContext = new ApplicationDbContext();
}
var result=_dbContext.people.where(p=> ...... ).contains()...
我该怎么办?感谢。
更新我自己编写了一些代码,但是没有用。我不能在哪里使用||使用双重条件
var result = _dbContext.people;
if (!string.IsNullOrEmpty(query)) result = result.Where(a => a.Name.Contains(query) || a.LastName.Contains(query));
答案 0 :(得分:0)
从您的评论中,听起来您想转换为方法语法。就个人而言,我发现LINQ语法更容易读写。但方法语法应如下所示:
var results = db.People
.Where(p => (p.FirstName + " " + p.LastName).Contains(query))
.Orderby(p => p.FirstName)
.ThenBy(p => p.LastName)
.Take(10)
.ToList();