Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am getting a bit confused with this and need a little help please. I am developing a ASP.Net MVC 3 Web application using Entity Framework 4.1.

I have a DropDownList on one of my Razor Views, and I wish to display a list of Full Names, for example

Tom Jones 
Michael Jackson 
James Brown

In my Controller I retrieve a List of User Objects, then select the FirstName and LastName of each User, and pass the data to a SelectList.

List<User> Requesters = _userService.GetAllUsersByTypeIDOrgID(46, user.organisationID.Value).ToList();

var RequesterNames = from r in Requesters
                     let person = new { UserID = r.userID, FullName = new { r.firstName, r.lastName } }
                     orderby person.FullName ascending
                     select person;

viewModel.RequestersList = new SelectList(RequesterNames, "UserID", "FullName");

return View(viewModel);

In my Razor View I have the following

@Html.DropDownListFor(model => model.requesterID, Model.RequestersList, "Select", new { @class = "inpt_a"})
@Html.ValidationMessageFor(model => model.requesterID)

However, when I run the code I get the following error

At least one object must implement IComparable.

I feel as if I am going about this the wrong way, so could someone please help with this?

Thanks.

share|improve this question

1 Answer

up vote 4 down vote accepted

In your LINQ query FullName should be declared like this, otherwise EF doesn't know how to order on the anonymous object you created:

FullName = r.firstName + " " + r.lastName

Example:

var RequesterNames = 
    from r in Requesters
    let person = new { UserID = r.userID, FullName = r.firstName + " " + r.lastName }
    orderby person.FullName ascending
    select person;
share|improve this answer
 
Excellent. Thanks for your help. –  tgriffiths Sep 26 '12 at 9:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.