score:3
Accepted answer
You may do any ordering. If column doesn't exist, just leave your input enumerable as it was before. To do this, create key selector that returns the same value for all elements.
See example:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
static class Program
{
public static IOrderedEnumerable<T> OrderBy<T>(this IEnumerable<T> list, string sortExpression) where T : class
{
Func<T, Int32> keySelector = (elem) =>
{
PropertyInfo pi = typeof(T).GetProperty(sortExpression, typeof(Int32));
if (pi == null)
return 0; // return the same key for all elements
return Int32.Parse(pi.GetValue(elem, null).ToString());
};
return list.OrderBy(keySelector);
}
static void Main(string[] args)
{
// Create an array of strings to sort.
string[] fruits = { "apricot", "orange", "banana", "mango", "apple", "grape", "strawberry" };
// Sort by "column" Length
foreach (string s in fruits.OrderBy<string>("Length"))
Console.WriteLine(s);
Console.WriteLine();
// Sort by non-existing column
foreach (string s in fruits.OrderBy<string>("ength"))
Console.WriteLine(s);
Console.ReadKey();
}
}
Source: stackoverflow.com
Related Articles
- IOrderedEnumerable and defensive programming
- LINQ Source Code Available
- .NET 4 Code Contracts: "requires unproven: source != null"
- creating Linq to sqlite dbml from DbLinq source code
- No Exists method so I want to use AsQueryable for defensive programming
- source code for LINQ 101 samples
- List or Array of String Contain specific word in Html Source Code
- c# Linq or code to extract groups from a single list of source data
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Value cannot be null. Parameter name: source
- Linq code to select one item
- C# - code to order by a property using the property name as a string
- How do I find the text within a div in the source of a web page using C#
- Roslyn failed to compile code
- Entity-framework code is slow when using Include() many times
- The data source does not support server-side data paging
- How are people unit testing code that uses Linq to SQL
- Entity Framework, Code First and Full Text Search
- What does this C# code with an "arrow" mean and how is it called?
- How to resolve Value cannot be null. Parameter name: source in linq?
- The source contains no DataRows
- Is it possible to turn an IEnumerable into an IOrderedEnumerable without using OrderBy?
- OrderBy and List vs. IOrderedEnumerable
- Could not find an implementation of the query pattern for source type 'System.Data.Entity.DbSet'
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- Is there an IEnumerable implementation that only iterates over it's source (e.g. LINQ) once?
- Entity Framework 6 Code First Custom Functions
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Is it possible to express this code in LINQ?
- how can i pass parameter for Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
- Linq: no return value caused exception
- LINQ - database query using "into", how do I reference data prior to "into" in the select clause?
- IEnumerable.Select() when attribute is known only at runtime
- How to do a wildcard DataTable select with lambda/LINQ using question marks?
- EF Core: filter condition for nested collection (Func<>) as a variable
- What would this sql query (w count and group by) look like when translated to linq?
- LINQ Group-by with complete object access
- Select value and flatten sublist of objects within a list of objects
- Linq to SQL Error Mapped
- Is there an easy way to return the FIRST LinqToXML result?
- Combine results after join and groupby with DataTable and List<Object>
- I have Entity Framework 6.2 I am trying to find all columns in a table that have a type of date time
- Better way to check for elements in list?
- Select unique/distinct combination of values from IEnumerable of same type
- Convert list to group
- How to use linq to return a boolean
- Get Field Value For Linq Query - SharePoint
- OPEN XML SDK Infinite Loop
- Excluding dates from Linq Query in MVC .net application