how to use order by in collection?

Feb 8, 2010 at 7:20 AM

hi,

i have a collection ProductsCollection productCollection = ProductsCollection.RunSelect(); 

how can i add order by in this collection?

 

thanks

Coordinator
Feb 8, 2010 at 1:32 PM

 

 

 

 

foreach (UserAccount item in userAccountCollection.OrderBy(x => x.PhoneHome))

{
}

You can order it with a lambda expression. The following code orders the collection and you loops through it.

 

foreach (Product item in ProductCollection.OrderBy(x => x.LastName))
{
//Do Something
}

 

Feb 8, 2010 at 2:07 PM

hi thanks for the reply 

is this correct?

ProductsCollection productCollection = ProductsCollection.RunSelect();
            GridView.DataSource = (from x in productCollection orderby x.Id descending select x);
            GridView.DataBind();

 

ProductsCollection productCollection = ProductsCollection.RunSelect();
GridView.DataSource = (from x in productCollection orderby x.Id descending select x);
GridView.DataBind();

 

 

Coordinator
Feb 8, 2010 at 6:28 PM

Yes that will work or you can just use the OrderBy extended method.

GridView.DataSource = productCollection.OrderByDescending(x => x.Id);

May 3, 2010 at 12:50 PM

hi how can i use orderby in this sample code: 

protected ProdImageGalleryCollection GetProductImages() {            
int prodId;            
int.TryParse(Request["prodId"], out prodId);            
ProdImageGalleryCollection prodImageGalleryCollection = ProdImageGalleryCollection.RunSelect(x=>x.ProductId == prodId);            
return prodImageGalleryCollection;

 

i try to use:
return prodImageGalleryCollection.OrderBy(x=>x.sort);

and this:
return (from x in prodImageGalleryCollection orderby x.Sort select x) 

still gives me error.

 

help pls..

 

regards,
John 

Coordinator
May 3, 2010 at 4:23 PM
LINQ returns anonymous classes which cannot be returned from methods. You can use them as a data source or use them locally in a method; however .NET does not allow passing these constructs around. This is a .NET framework limitation and is documented.
May 4, 2010 at 3:13 AM

yeah your right i just discoverd the limitation. i already solve the problem.

 

thanks for the reply

 

regards,

john