473,545 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Returning data contained in Ienumerable<typ e> as type[]?

Hi there,

Does anybody know how to return the results of an IEnumerable<typ eas an
array of the same type i.e type[] in a web service call without first having
to collect all elements in the IEnumerable<typ eand storing it in memory
first?

This question is really about optimizing large collections of data returned
by web services. If it is possible to loop through the IEnumerable<typ e>
with a "foreach" and somehow start formatting the soap response already,
there should be a fair amount of memory and time saved. We are currently
running into timeouts and outofmemoryExce ptions because building up a
collection of types in a list<typecannot handle the load.

I tried to do this using the Context.Respons e and the formatting the SOAP
message myself, but the result of the call on the client returns a null
value. I suspect that this is because it expects a compressed SOAP message.
I also tried to compress the response using GZipStream, but to no avail.

Any help on this would be greatly appreciated.
Dec 21 '06 #1
1 2936
Peter,

Thanx for the response.

I have a stored procedure which I execute via a DataReader. The method that
reads the data from the stored procedure returns structs with the "yield
return" method as a generic IEnumerable for example:

public IEnumerable<Cus tomerStructGetC ustomers()
{
IdataReader Mydata = DataLayer.Execu te("ProcedureNa me");
while (Mydata.Read())
{
yield return new CustomerStruct( MyData.GetStrin g(0).......);
}
}

I call this method from within a web service. The problem is that a web
service cannot expose IEnumerable<Cus tomerStruct>, so I have to use a
CustomerStruct[].

In order to get the results from the GetCustomers method returned, I first
have to get all the CustomerStruct structs in a single collection, which
takes up to much memory and makes things slow.

I would prefer a way to start building up the resulting soap message that
goes to the client while I am iterating through the method's resulting
IEnumerable.

If this can be done, it means that I will always have only one
CustomerStruct and one datarow in memory at a given time, which will be much
faster than having to collect all CustomerStruct' s first.

I am currently doing this as follows, but it is to slow:

List<CustomerSt ructResult = new List<CustomerSt ruct>(GetCustom ers());
return Result.ToArray( );

I hope this makes more sense now.

"Peter Ritchie [C# MVP]" wrote:
No, IEnumerable only provides a means to enumerate, not convert (hence the
"Enumerable "). IEnumerable is merely an interface to an implementation which
probably has something you can use to convert to an array. Where are you
getting this IEnumerable interface instance? Are you implementing this
collection yourself or are you retrieving it from an 3rd party method?

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"FrancoisViljoe n" wrote:
Hi there,

Does anybody know how to return the results of an IEnumerable<typ eas an
array of the same type i.e type[] in a web service call without first having
to collect all elements in the IEnumerable<typ eand storing it in memory
first?

This question is really about optimizing large collections of data returned
by web services. If it is possible to loop through the IEnumerable<typ e>
with a "foreach" and somehow start formatting the soap response already,
there should be a fair amount of memory and time saved. We are currently
running into timeouts and outofmemoryExce ptions because building up a
collection of types in a list<typecannot handle the load.

I tried to do this using the Context.Respons e and the formatting the SOAP
message myself, but the result of the call on the client returns a null
value. I suspect that this is because it expects a compressed SOAP message.
I also tried to compress the response using GZipStream, but to no avail.

Any help on this would be greatly appreciated.
Jan 2 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4363
by: Doug Dew | last post by:
This won't compile: using IEnumerable<T> = System.Collections.Generic.IEnumerable<T>; namespace MyNamespace { public class MyClass<T> : IEnumerable<T> { // Appropriate stuff here }
10
2735
by: jcc | last post by:
Hi guys, I'm a newbie to C#. My Visual Studio 2005 failed to compile the following code with error as 'HelloWorld.A' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'. 'HelloWorld.A.GetEnumerator()' is either static, not public, or has the wrong return type. class A : IEnumerable<string>
2
19855
by: Steve Richter | last post by:
very confused on how to implement the IEnumerable and IEnumerator interfaces on a generic type. I understand I should just use a foreach loop in the GetEnumerator method and use "yield return", etc. would like to know how implement my generic GetEnumerator to yield return an interface to IEnumerator<T>. I am getting a compile error that...
2
7343
by: Fred Heida | last post by:
Hi, i'm trying to (using managed C++) implment the IEnumerable<Tinterface on my class.. but have a problem with the 2 GetEnumerator method required.... what i have done is... generic<typename T> public ref class SetOfProxy : public System::Collections::Generic::IEnumerable<T>
1
2009
by: =?Utf-8?B?UElFQkFMRA==?= | last post by:
Something I found surprising this week involves the IEnumerable<Tinterface. I have a class that I wrote a couple of years ago which implements the IEnumerable interface. This week I realized it should implement IEnumerable<T>. But when I changed the return type of the GetEnumerator method the compiler said the class no longer implemented...
5
11139
by: Ronald S. Cook | last post by:
From my business tier (class) I get back an IQueryable<Penof data. Here is my client code that works fine: PenClass penClass = new PenClass(); IQueryable<Penpens = penClass.SelectPens(); However, I want to then select a subset of this data from within the client. I tried this: IQueryable<PenpensSubset = from p in pens select p.PenId,...
3
1925
by: Peter Morris | last post by:
I've given this interface some thought, the benefits to using it seem to be 01: I can use "var" in a foreach //Works List<SomeClasslist = new List<SomeClass>(); foreach(var item in list) item.MethodOnSomeClass(); //Doesn't work
5
1786
by: Anders Borum | last post by:
Hello I'm developing an API with a number of concrete collections that inherit from an abstract collection hierarchy. Each inheritance level provides an implementation of IEnumerable<T>, thus allowing the developer to take full advantage of the LINQ extensions without having to start with a cast operation to a concrete type that matches the...
6
14540
by: A.Rocha | last post by:
Hi, I need save to text file a List<type>, but i dont wont serialize. // List<mytypemyList = new List<mytype>(); anyone can help me? --
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7413
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7751
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4943
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3440
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1874
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.