Run Code
|
API
|
Code Wall
|
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
Round Program
ew
Types
Error
T
Armstrong
QQW
Converting a string to byte-array without using an encoding (byte-by-byte)
c# yamuk çizgi
UrlAccess