views:

93

answers:

4
 var pom= from k in dataContext.student_gods
                            where k.skgod==System.Convert.ToString(2002/03)
                            select k;
            foreach(var i in pom){
            var predmeti = from m in dataContext.student1s
                           where m.id_stud ==System.Convert.ToString(i.id_stud)

                           select m;


            }
          return View(predmeti);

This is code after suggestions. I have one error: the name predmeti does not exist in current context. If declare var predmeti before foreach loop I dont know how to initialize

+3  A: 

The return is in the middle of the foreach loop. I am not sure what the context is but you may want to yield return instead of build the view with the whole collection.

In this case, you exit from the method after the first item in the collection in which you're iterating.

Elisha
A: 

Is it because you are returning the function in the foreach loop?

foreach(var i in pom){
            var predmeti = from m in dataContext.student1s
                           where m.id_stud ==System.Convert.ToString(i.id)

                           select m;

            return View(predmeti);
}

This means that the function will quit at the first i.

Ngu Soon Hui
+1  A: 

This code line looks really odd to me:

where k.skgod==System.Convert.ToString(2002/03);

This means that you will get all records where k.skgod = 667. Is this your intention?

Still, as others have already mentioned; the return in your foreach will effectively prevent the code from returning anything else than the first object in the collection.

Fredrik Mörk
This means that I will get all students from 2002/03 year
Ognjen
So, all students from 2002/03 year has `skgod = 667`?
Fredrik Mörk
column in data table : id_stud first_name last_name skgod ....
Ognjen
+1  A: 

I think you are over-complicating this, and risking the infamous foreach/capture issue (since LINQ uses deferred execution). Perhaps just:

var pom= from k in dataContext.student_gods
         where k.skgod==System.Convert.ToString(2002/03)
         select k.id_stud;
var list = pom.ToList();
var predmeti = from m in dataContext.student1s
               where list.Contains(m.id_stud)
               select m;
return View(predmeti);
Marc Gravell
Now I tried but it does not return data from table
Ognjen
Well, is there any data in the list? Since we can't reproduce your setup you're going to have to help us a bit...
Marc Gravell
how I check is list empty?? I think the problem is in the list
Ognjen
I hvae create View or what I do
Ognjen