Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Linq OrderBy filtering null or empty values to be last

I try to make my custom orderby extension method, i successfully worked my code but in addition i want to list null or empty or zero values last in result, anyone can help me about that issue ?

Here is my extension method to orderby

    public static IQueryable<T> OrderBy<T>(this IQueryable<T> q, string SortField, bool isAsc)     {         //var nullExpr = Expression.Constant(null, typeof(T));         var param = Expression.Parameter(typeof(T), "p");         var prop = Expression.Property(param, SortField);         var exp = Expression.Lambda(prop, param);         string method = isAsc ? "OrderBy" : "OrderByDescending";         Type[] types = new Type[] { q.ElementType, exp.Body.Type };         var mce = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);         return q.Provider.CreateQuery<T>(mce);     } 

Thanks in advance

like image 958
Cihan Uygun Avatar asked Nov 28 '12 12:11

Cihan Uygun


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

The simplest way is to use

OrderBy(e => String.IsNullOrEmpty(e.TeamName) 

This doesn't require any extension method or custom IComparer implementation etc.

var entries = repository.Race.Where(e => e.EventId == id)                       .OrderBy(e => String.IsNullOrEmpty(e.TeamName))                       .ThenBy(e => e.LastName)                       .ThenBy(e => e.FirstName); 
like image 174
Rajes Avatar answered Sep 16 '22 12:09

Rajes


Without using an extension method....

Create a custom IComparer<string> to check the empty values before using the default String.Compare. The first checks will return -1 instead of 1 or 1 instead of -1, if using the standard string comparison.

/// <summary> /// Returns -1 instead of 1 if y is IsNullOrEmpty when x is Not. /// </summary> public class EmptyStringsAreLast : IComparer<string> {     public int Compare(string x, string y)         {             if (String.IsNullOrEmpty(y) && !String.IsNullOrEmpty(x))             {                 return -1;             }             else if (!String.IsNullOrEmpty(y) && String.IsNullOrEmpty(x))             {                 return 1;             }             else             {                 return String.Compare(x, y);             }         }  } 

Pass your EmptyStringsAreLast comparer into the OrderBy of Lambda expression. In this solution teams who have entered the race should appear alphabetical order, but the unaffiliated race entries should appear at then end.

var entries = repository.Race.Where(e => e.EventId == id)                           .OrderBy(e => e.TeamName, new EmptyStringsAreLast())                           .ThenBy(e => e.LastName)                           .ThenBy(e => e.FirstName); 
like image 23
Dave Anson Avatar answered Sep 16 '22 12:09

Dave Anson