---
title: "Sorting using lamba expression"  
description: "Sorting using lamba expression"  
author: "Anonymous User"  
published: 2014-03-26  
updated: 2014-03-26  
canonical: https://www.mindstick.com/forum/1993/sorting-using-lamba-expression  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Sorting using lamba expression

I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to Sort Users Using [lambda Expression](https://www.mindstick.com/forum/159550/how-to-implement-an-interface-with-two-abstract-methods-by-a-lambda-expression). My Users [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp)

```
public class Users{        public long Id { get; set; }        public string UserName { get; set; }        public string Password { get; set; }        public string FirstName { get; set; }        public string LastName { get; set; }        public DateTime DateCreated { get; set; }}
```

And My [Sorting](https://www.mindstick.com/articles/1581/sorting-list-with-side-bar) [method](https://www.mindstick.com/forum/166/webservice-method)

```
public List<Users> SortUsers(string sSortBy){    var arg = Expression.Parameter(typeof(Users), "Users");    var body = Expression.Convert(Expression.Property(arg, sSortBy), typeof(object));    var lambda = Expression.Lambda<Func<Users, object>>(body, arg);    List<Users> UserList;    UserList = UOWUser.UsersRepository.Entities.OrderBy(lambda).ToList(); // Error here    return UserList;}
```

I am Accessing the method by

List<Users> UserList;

UserList = objUsers.SortUsers("[FirstName](https://www.mindstick.com/interview/33973/how-to-split-the-full-name-into-firstname-and-lastname-in-sql-server)");

I am passing sSortBy as [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) (here "FirstName")

[Error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing) :Unable to cast the type 'System.String' to type 'System.Object'.

Comes in line UserList = UOWUser.UsersRepository.Entities.OrderBy(lambda).ToList();

## Replies

### Reply by Pravesh Singh

Hi Ankita,\

```
public static List<Users> SortUsers(string sSortBy){    var arg =Expression.Parameter(typeof(Users), "Users");    var sortProperty =Expression.Property(arg, sSortBy);    var lambda =Expression.Lambda(sortProperty, arg);    var param =Expression.Parameter(typeof(IQueryable<Users>));    var orderByCall =Expression.Call(typeof(Queryable), "OrderBy", new Type[] {
typeof(Users), sortProperty.Type }, new Expression[] { param, lambda });    var orderLambda =Expression.Lambda<Func<IQueryable<Users>,
IQueryable<Users>>>(orderByCall, param).Compile();    List<Users>
UserList;    UserList =orderLambda(UOWUser.UsersRepository.Entities).ToList(); // Error here    return UserList;}
```

It makes the entire source.OrderBy(x => x.PropName) through Expression, so you don't have to actually specify x.PropName type.


---

Original Source: https://www.mindstick.com/forum/1993/sorting-using-lamba-expression

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
