Loop though IList, but with for loop, not for each
Solution 1
Since you are using a non-Generic IList, you are required to cast the value:
for (int i = 0; i < results.Count; ++i)
{
Console.WriteLine(((NHibernate.Examples.QuickStart.User)results[i]).EmailAddress); // Not Working
}
Alternatively, you could make your IList the Generic version by changing the 1st line to:
System.Collections.IList<NHibernate.Examples.QuickStart.User> results = crit.List();
Note that for this solution, you would have to change the crit.List() function to return this type.
Solution 2
You are using a basic IList, which store objects as type Object
. If you use a foreach
, type casting is done for you automatically. But if you use an indexer as in for (i = 0; i<count...
, it is not.
Try this, see if it works:
for (int i = 0; i < results.Count; ++i)
{
var result = (NHibernate.Examples.QuickStart.User)results[i];
Console.WriteLine(result.EmailAddress); // Not Working
}
...
Solution 3
for (int i = 0; i < results.Count; ++i)
{
Console.WriteLine((NHibernate.Examples.QuickStart.User)results[i]).EmailAddress); // Not Working
}
Remember to cast the element type properly, since the IList
indexer returns a simple object
.
Stefan Steiger
I'm an avid HTTP-header-reader, github-user and a few more minor things like BusinessIntelligence & Web Software Developer Technologies I work with: Microsoft Reporting- & Analysis Service (2005-2016), ASP.NET, ASP.NET MVC, .NET Core, ADO.NET, JSON, XML, SOAP, Thrift ActiveDirectory, OAuth, MS Federated Login XHTML5, JavaScript (jQuery must die), ReverseAJAX/WebSockets, WebGL, CSS3 C#, .NET/mono, plain old C, and occasional C++ or Java and a little Bash-Scripts, Python and PHP5 I have a rather broad experience with the following relational SQL databases T-SQL PL/PGsql including CLR / extended stored procedures/functions Occasionally, I also work with MySQL/MariaDB Firebird/Interbase Oracle 10g+ SqLite Access I develop Enterprise Web-Applications (.NET 2.0 & 4.5) and interface to systems like LDAP/AD (ActiveDirectory) WebServices (including WCF, SOAP and Thrift) MS Federated Login OAuth DropBox XML & JSON data-stores DWG/SVG imaging for architecture In my spare-time, I'm a Linux-Server-Enthusiast (I have my own Web & DNS server) and reverse-engineer with interest in IDS Systems (IntrusionDetection), WireShark, IDA Pro Advanced, GDB, libPCAP. - Studied Theoretical Physics at the Swiss Federal Institute of Technology (ETHZ).
Updated on June 04, 2022Comments
-
Stefan Steiger almost 2 years
I have a IList of objects. They are of type NHibernate.Examples.QuickStart.User. There is also an EmailAddress public string property.
Now I can loop through that list with a for each loop.
Is it possible to loop through a Ilist with a simple for loop?
Because simply treating the IList as an array doesn't seem to work...System.Collections.IList results = crit.List(); foreach (NHibernate.Examples.QuickStart.User i in results) { Console.WriteLine(i.EmailAddress); } for (int i = 0; i < results.Count; ++i) { Console.WriteLine(results[i].EmailAddress); // Not Working }