Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using (School_365Entities db = new School_365Entities())
           {
               var Query = (from x in db.tbl_staticfielddetails
                            where x.institutionid == insid
                            select x.id).Count();

               foreach (var item in Query)
               {
                   cnt = Convert.ToInt32(item.ToString());

               }

           }







I got the error
SQL
Error   foreach statement cannot operate on variables of type 'int' because 'int' does not contain a public definition for 'GetEnumerator' 
Posted
Comments
Аslam Iqbal 9-Jul-14 5:27am    
var Query is int type variable not a list. Query represents the count of the written LINQ. Remove the .Count() & try again, foreach will work for Query.

1.Your code above return only one results of type integer (the count result) and you cannot iterate into one it.

2.For examples of LINQ expresions that are using almost all possible aggregate functions see the next MSDN link:
http://code.msdn.microsoft.com/LINQ-Aggregate-Operators-c51b3869[^]
 
Share this answer
 
 
Share this answer
 
you can directly get the count as below
SQL
int count = (from x in db.tbl_staticfielddetails
                            where x.institutionid == insid
                            select x.id).Count();


reason is Count [^] method return type is int
 
Share this answer
 
v2
C#
var Query = (from x in db.tbl_staticfielddetails
                           where x.institutionid == insid
                           select x.id).Count();

here your query returns a integer value. So why are you putting this inside a loop?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900