Help please: How do I use the WebDataQuery<T>.Count<T> funcntion?

Hi all,

I'm a .Net noob so bear with me here OK. This is how I learnSmile

I have used WebDataQuery<T> to return some data from an Astoria service. The data has been returned in XML format.

Now, what exactly can I use the Count<T> method for? I know what the XML contains, its basically a flat list of elements, cna I use this method to tell me how many?

Also, I'm not even sure its even a method. If I look in Object Browser there is no Count<T> method on the WebDataQuery<T> class. The symbol for it in the Intellisense menu is a little differrent from normal, and the tooltip gives some gibberish (to me anyway) about (extension) int IEnumerable blah blah blah. Its also not available in the immediate window - so I'm very confused as to what this Count<T> thing actually is. i assume is something to do with generics - something I don't grok at all yet.

If someone could fill in the blanks for me here I would be most grateful.

Also, is there an easy way to convert the returned data into an XML document (if indeed it has been returned in XML format)?

Thanks in advance

Jamie

[1317 byte] By [JamieThomson] at [2008-2-3]
# 1

Hi,

Let me first make sure I understand how you are using the API. Are you using WebDataQuery and then doing a for-each over it? That API returns .NET objects, not XML, so I wanted to make sure we're talking about the same entry point.

If you are using WebDataQuery, you can use count (e.g. query.Count()) to count the number of elements, but keep in mind that doing so will bring the elements to the client and then count them there, so if you have a lot it may be a costly way of doing it.

If you need to do this often and over a lot of elements, you may want to consider data-aware service operations. The Astoria reference document ("Using Astoria") discusses them and shows an example.

Pablo Castro

Technical Lead

Microsoft Corporation

http://blogs.msdn.com/pablo

PabloCastro-MSFT at 2007-10-2 > top of Msdn Tech,Incubation Technologies,Project Codename: Astoria...
# 2
Pablo Castro - MSFT wrote:

Hi,

Let me first make sure I understand how you are using the API. Are you using WebDataQuery and then doing a for-each over it? That API returns .NET objects, not XML, so I wanted to make sure we're talking about the same entry point.

Spot on Pablo. I understand the API exposes everything as objects but I would still have hoped there would be a collection that I could iterate over.

Pablo Castro - MSFT wrote:

If you are using WebDataQuery, you can use count (e.g. query.Count()) to count the number of elements, but keep in mind that doing so will bring the elements to the client and then count them there, so if you have a lot it may be a costly way of doing it.

OK, well I've already got the data at the client end in an object which is an instantiation of WebDataQuery<T>. There is no Count() method on that object, only Count<T>() and I don't understand how to use that. If you could give me some pointers I would be very very grateful. Thanks.

Pablo Castro - MSFT wrote:

If you need to do this often and over a lot of elements, you may want to consider data-aware service operations. The Astoria reference document ("Using Astoria") discusses them and shows an example.

Pablo Castro

Technical Lead

Microsoft Corporation

http://blogs.msdn.com/pablo

I'll take a look at the doc, thanks Pablo.

-Jamie

JamieThomson at 2007-10-2 > top of Msdn Tech,Incubation Technologies,Project Codename: Astoria...
# 3

Note that WebDataQuery<T> represents a query formulation, not the results. Whenever you enumerate over it (or call Execute) you are getting the data from the server.

The Count<T>() method you are seeing is an extension method. You can all it as a regular method...if your query object is called "query", you should be able to say "query.Count()". If you want both the data and the total count, you can first push the results from the query into a List object by calling ToList(), and then checking out the count on the list object.

Pablo Castro

Technical Lead

Microsoft Corporation

http://blogs.msdn.com/pablo

PabloCastro-MSFT at 2007-10-2 > top of Msdn Tech,Incubation Technologies,Project Codename: Astoria...
# 4
Pablo Castro - MSFT wrote:

Note that WebDataQuery<T> represents a query formulation, not the results. Whenever you enumerate over it (or call Execute) you are getting the data from the server.

The Count<T>() method you are seeing is an extension method. You can all it as a regular method...if your query object is called "query", you should be able to say "query.Count()". If you want both the data and the total count, you can first push the results from the query into a List object by calling ToList(), and then checking out the count on the list object.

Pablo Castro

Technical Lead

Microsoft Corporation

http://blogs.msdn.com/pablo

AHA. I see. Thanks very much Pablo. i guess I should RTFM shouldn't I? Sorry to bother you.

I don't currently understand what extension methods are but as and when I need to know I'll look into it.

Thanks again.

-Jamie

JamieThomson at 2007-10-2 > top of Msdn Tech,Incubation Technologies,Project Codename: Astoria...