我正在使用linq进行搜索功能。我必须通过一系列地点进行搜索(来自Tokio,柏林,纽约的展示学生)。我有一个foreach语句,它通过所有的位置并将它们添加到列表中。我的问题是,我无法对所有这些事情都置之不理。如何在foreach之前声明var newstudents?达到foreach之外的变量
贝娄是我的代码
public void search(IEnumerable<string> Location)
{
foreach (var l in Location)
{
var students = from s in db.Students select s;
students = students.Where(s => s.City.Contains(l));
var customers = students.ToList();
}
int custIndex = 1;
Session["TopEventi"] = customers.ToDictionary(x => custIndex++, x => x);
ViewBag.TotalNumberCustomers = customers.Count();
声明'VAR的客户= students.ToList();'前'foreach' –
抱歉,但学生们declaired forecah –
里面然后不!无论如何,所有这些都可以在一行代码中完成。 'var students = db.Students.Where(x => x.City.Any(x => Location.Contains(x));' –