This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi New to LightSpeed and Linq, what's the basic syntax for returning a recordset / collection of objects, or whatever you want to call it? And then iterating through them. Something like: ProductSet productset = uow.Products.Select(p => p.categoryID == 20); foreach(Product product in productset){ ... etc etc.... }
Many thanks, Danzig |
|
|
Hi Danzig, What you have written is pretty much it :-) In the getting started screencast we demonstrate how print all the products from a database to the console. That should help. http://www.mindscape.co.nz/products/lightspeed/screencasts/gettingstarted.aspx I hope that helps, John-Daniel |
|
|
Hi John-Daniel Thanks a lot for the quick reply. I have watched that screencast and that's where I got the general idea for the above. But I don't know what type of container is returned which I would then iterate through to get each entity instance. Like if the query returns 20 rows, how do I step through them? ie. Where I've got "ProductSet productset = uow.Products.Select(p => p.categoryID == 20);", there's obviously no such thing as "ProductSet". Is it supposed to be IList<product> or something like that? Cheers, Danzig |
|
|
Hi Danzig, You will actually get an IQueryable result which is a somewhat magic collection. Until you actually iterate over it a query won't be issued to the database. This means you can apply additional Where() clauses (and others) to an IQueryable and it won't trigger a query until you iterate the collection. It effectively will behave the same as an IList<Product> for the most part however if you want to force the query to execute and the results to be returned as an IList<Product> then put a .ToList() on the end of your query being made (after the Select() call). It's a bit strange to get used to if you have not used IQueryable before but it works out pretty well as something that you don't often need to care too much about. So you can just iterate through the results and for each item you will get a Product returned. Let me know if you want any more guidance. John-Daniel Trask |
|
|
Hi John-Daniel Thanks for that. I don't think I've quite tapped into the magic yet though. ;) Here's what I've got: IQueryable productSet = uow.Products.Select(p => p.categoryID == 20); That gives me an "Incorrect syntax near the keyword 'FROM'." error when entering the foreach loop. Can you advise? Is the "Select" method the right one to use? (I just substituted that in as the best looking alternative for retrieving more than one record as opposed to the "Single" used in the getting started webcast. Cheers and thanks, Danzig |
|
|
Hi Danzig, Select will let you project out properties (it's a bit confusing if you're not used to LINQ so I can understand the mistake!). What you want to do is this: var productSet = uow.Products.Where(p => p.categoryID == 20); That will select all products where they belong to category 20. I believe the invalid syntax error will be because you're not actually writing a Select() query as they're meant to be expressed however if the problem does not go away once you change your query to what I have here then please let me know :-) John-Daniel |
|
|
The way to remember this, by the way, is by analogy with SQL. In SQL, SELECT chooses which "columns" to return (which may include computed expressions), and WHERE chooses which rows to return those columns for. Same in LINQ. Select chooses what "properties" to return (which may include computed expressions), and Where chooses which entities to return those properties for. Thus, in your example, your Select expression was returning a computed expression of whether the category ID was equal to 20, for all products. (And the syntax error occurred because LightSpeed was trying to translate this computation into SQL and was not able to do so.) |
|
|
Awesome, thanks guys, I see what you mean and it's working perfectly now. |
|
|
One more question about this: How do I test if "productSet" contains any results? Thanks again, Danzig |
|
|
You shouldn't get an error in that case -- the loop body won't execute (because there are no products on which to execute it), but you shouldn't get an error. What's the exception message and can you provide a stack trace? Thanks! |
|
|
My bad. I was passing a querystring value into the query and wasn't checking that the querystring value existed and misunderstood the error message. Anyway, your reply got me to find the prob so thx for that. |
|