OrderBy Error

Mar 8, 2011 at 6:55 PM
Edited Mar 8, 2011 at 7:22 PM

I just started using nHydrate and I love the features. But when I wanted to get the top 10 items (ordered by CreatedDate) of a table, I ran into an error:

 Cannot implicitly convert type 'System.Linq.IOrderedEnumerable' to 'Facturatie.Business.Objects.ClientCollection'. 
An explicit conversion exists (are you missing a cast?)

My code:

 ClientCollection clients = ClientCollection.RunSelect().OrderBy(x => x.CreatedDate).Take(10);

I'm not sure if I'm doing it correct, because this is the first time I work with LINQ. Eager to learn more about it though.


EDIT: Now that I compile my code, I get errors about Widgetsphere.Core... Do I need to include a reference to solve this?

Example: Error 1 The type 'Widgetsphere.Core.DataAccess.IWrappingClass' is defined in an assembly that is not referenced. You must add a reference to assembly 'Widgetsphere.Core, Version=, Culture=neutral, PublicKeyToken=5cb8d3744e1198c9'. D:\My Documents\My Projects\Facturatie\Facturatie\WinForms\Intro.cs

Mar 8, 2011 at 8:39 PM

First off I do not know how you got that to compile. Are you sure that is runnable code? The Skip/Take methods do not return the ClientCollection back. They return IEnumerable<Client>. I ran the code below and it is fine. 

var list = AdministratorCollection.RunSelect().OrderBy(x => x.CreatedDate).Skip(10).Take(10).ToList();
Please change your code a little to use the IEnumerable or "var"

And yes, you need to reference Widgetsphere.Core.dll

Mar 9, 2011 at 7:47 AM
Edited Mar 9, 2011 at 11:03 AM

Thank you for your swift and precise help. I forgot to add the .ToList() and I have added Widgetsphere.Core.dll to my project references.

One last thing: I'm trying to bind the list to a  ListBox, which works like a charm. I set the DisplayMember to "name" and the ValueMember to "client_id". The names are displayed properly, but the ValueMember returns the string "client_id" instead of the actual id (an int).


var list = ClientCollection.RunSelect().OrderBy(x => x.CreatedDate).Take(10).ToArray();
listBoxClients.DisplayMember = "name";
listBoxClients.ValueMember = "client_id";
listBoxClients.DataSource = list;

EDIT: Fixed it, I didn't know the whole object would be imported into the listbox. I can just get it out with a simple line that converts the selected item to a client. Didn't even need the valuemember. /blush