Sandi Horvat
Sandi Horvat

Reputation: 497

reach variable outside of foreach

I'm using linq for search function. I have to search by a list of locations(display students from Tokio,Berlin, New York). I have a foreach statement, which is going throu all the locations and adds them to a list. My problem is that I can't dispaey them all outside of foreach. How can I declare var newstudents before foreach?

Bellow is my code

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();

Upvotes: 0

Views: 191

Answers (4)

azrael.pl
azrael.pl

Reputation: 85

you can declare a dictionary for mapping your new student to specific location, and add new lists to it in the loop.

also your use of the word newstudents is a bit confusing - you're nor lookning here new students in your code only map their location. Anyways: considerung new students from outside the loop:

public void search(IEnumerable<string> Location)
{
    Dictionary<Location, List<Students>> newStudents = new  Dictionary<Location, List<Students>>();
    foreach (var l in Location)
    {
        var students = from s in db.Students select s;
        students = students.Where(s => s.City.Contains(l));
        newStudents[l]= students.ToList();                  
     }
     int custIndex = 1;
    //what is this for?  seeing lastly added 
     Session["TopEventi"] = customers.ToDictionary(x => custIndex++, x => x);
     ViewBag.TotalNumberCustomers = (from  lists in newStudents select  lists.Count).Sum();

Upvotes: 0

Barry O&#39;Kane
Barry O&#39;Kane

Reputation: 1187

Get rid of the loop entirely.

public void search(IEnumerable<string> Location)
{
       string[] locations = Location.Cast<string>().ToArray();
       var customers = db.Students.Where(s => locations.Contains(s.City)).ToList();

Upvotes: 2

Tim Schmelter
Tim Schmelter

Reputation: 460058

My problem is that I can't display them all outside of foreach. How can I declare var newstudents before foreach?

Why can't you do that? You just need to declare the variable as IEnumerable<ClassName>:

IEnumerable<Student> customers = null;
foreach (var l in Location)
{
    var students = from s in db.Students
                   where s.City.Contains(l)
                   select s;

    customers = customers.Concat(students);                  
}
customers = customers.ToList()

But you don't need the foreach at all, you can do it with one LINQ query:

IEnumerable<Student> customers = db.Students
    .Where(s => Location.Any(l => s.City.Contains(l)));

This approach is searching for a substring in Student.City which is the location.

Upvotes: 3

Constantin Treiber
Constantin Treiber

Reputation: 420

You could declare the List outside the foreach and in side you only do something like

yourList.AddRange(students.ToList());

Upvotes: 1

Related Questions