Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Linq query returns only the first sequence in a list in C#

$
0
0

Hi,

Could anyone please tell me whats wrong with the below Linq query? It iterate only first sequence 3 times based on CatNo 1. Means, CategoryNoList is 1,2,3, the query returns 3 same rows where CatNo is 1. But it should return 3 rows for each 3 CatNo.

Note: CatName is string and CatNo is int?

using (DefaultConnection dbCon = new DefaultConnection())
            {
                List<Products> prod = new List<Products>();
                var CategoryNoList = DefaultConnection.Products.Where(p => p.CatName == "catname").Select(p => p.CatNo).ToList();

                foreach (var catno in CategoryNoList)
                { 
                    var ProdList = (from ProdRec in dbCon.Products
                                    where ProdRec.CatName == "catname" && ProdRec.CatNo == catno
                                    select ProdRec).First();
                    prod.Add(ProdList);
                }
                return View(prod.ToList());
            }


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>