Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Conditional linq operators
//Title of this code //Rextester.Program.Main is the entry point for your code. Don't change it. using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { var list = new List<Test>() { new Test(1, 2), new Test(1, 5), new Test(1, 3), new Test(1, 4), new Test(1, 1), new Test(1, 3), new Test(2, 2), new Test(2, 5), new Test(2, 3), new Test(2, 4), new Test(2, 1), new Test(2, 3) }; Console.WriteLine("Orderer by one, then by two"); list.OrderBy(f => f.One).ThenByIf(true, f => f.Two).ToList().ForEach(f => Console.WriteLine(f)); Console.WriteLine("Orderer by one only"); list.OrderBy(f => f.One).ThenByIf(false, f => f.Two).ToList().ForEach(f => Console.WriteLine(f)); } } public class Test { public int One {get; set;} public int Two {get; set;} public Test(int one, int two) { One = one; Two = two; } public override string ToString() { return string.Format("One: {0}, Two: {1}", One, Two); } } public static class Ext { public static IOrderedEnumerable<T> ThenByIf<T, TKey>(this IOrderedEnumerable<T> list, bool predicate, Func<T, TKey> sel) { if(predicate) return list.ThenBy(f => sel(f)); else return list; } } }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
Convert string to TimeSpan in C#
IF... ELSE
Fibonacci recursion
TEst
Boo socks
simultQ
Fórum ➡ My Category Attribute ↔ New Default Attribute: Behavior ♦
uniqueness 2.8
Insertion Sort
Stack as a queue
Please log in to post a comment.