Created
November 19, 2019 08:12
-
-
Save christianhelle/869f892adf97ffada3199bbf4bf99c30 to your computer and use it in GitHub Desktop.
LinqExtensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Collections.Specialized; | |
using System.Configuration; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Runtime.InteropServices.WindowsRuntime; | |
namespace System.Linq.Extensions | |
{ | |
public static class LinqExtensions | |
{ | |
public static IEnumerable<T> Traverse<T>(this IEnumerable<T> source, | |
Func<T, IEnumerable<T>> selector) | |
{ | |
foreach (var item in source) | |
{ | |
yield return item; | |
var children = selector(item); | |
foreach (var child in children.Traverse(selector)) | |
{ | |
yield return child; | |
} | |
} | |
} | |
public static bool IsNullOrEmpty<T>(this IEnumerable<T> source) | |
{ | |
try | |
{ | |
return source == null || !source.Any(); | |
} | |
catch (Exception e) | |
{ | |
Debug.WriteLine(e); | |
return false; | |
} | |
} | |
public static bool ContainsAny<T>(this IEnumerable<T> source, params T[] items) | |
{ | |
try | |
{ | |
var list = source.ToList(); | |
return !list.IsNullOrEmpty() && | |
items.Any(t => list.Any(c => Equals(c, t))); | |
} | |
catch (Exception e) | |
{ | |
Debug.WriteLine(e); | |
return false; | |
} | |
} | |
public static string GetValueOrDefault(this Dictionary<string, string> dictionary, | |
string key, | |
string defaultValue = null) | |
{ | |
string value; | |
return dictionary.TryGetValue(key, out value) ? value : defaultValue; | |
} | |
public static string GetValueOrDefault(this NameValueCollection collection, | |
string key, | |
string defaultValue = null) | |
=> collection.AllKeys.Contains(key) ? collection[key] : defaultValue; | |
public static string GetValueOrDefault(this KeyValueConfigurationCollection collection, | |
string key, | |
string defaultValue = null) | |
=> collection.AllKeys.Contains(key) ? collection[key].Value : defaultValue; | |
public static bool GetBoolValue(this NameValueCollection collection, | |
string key, | |
bool defaultValue = false) | |
{ | |
bool result; | |
var value = GetValueOrDefault(collection, key, defaultValue.ToString()); | |
return bool.TryParse(value, out result) && result; | |
} | |
public static int GetInteger(this NameValueCollection collection, | |
string key, | |
int defaultValue = default(int)) | |
{ | |
int result; | |
var value = GetValueOrDefault(collection, key, defaultValue.ToString()) | |
.Replace(" ", string.Empty) | |
.Replace(",", string.Empty) | |
.Replace(".", string.Empty); | |
int.TryParse(value, out result); | |
return result; | |
} | |
public static void ReplaceAll<T>(this List<T> list, IEnumerable<T> items) | |
{ | |
list.Clear(); | |
list.AddRange(items); | |
} | |
public static IEnumerable<IList<T>> Slice<T>(this IEnumerable<T> enumerable, int chunkSize) | |
{ | |
var slice = enumerable as IList<T> ?? enumerable.ToList(); | |
if (chunkSize > slice.Count()) | |
yield return slice; | |
else | |
{ | |
var chunk = new List<T>(); | |
foreach (var item in slice) | |
{ | |
chunk.Add(item); | |
if (chunk.Count < chunkSize) | |
continue; | |
yield return chunk; | |
chunk = new List<T>(); | |
} | |
if (chunk.Count > 0) | |
yield return chunk; | |
} | |
} | |
public static ICollection<T> ToList<T>(this IEnumerable<T> source, bool nullIfEmpty) | |
{ | |
var list = source?.ToList(); | |
return list.IsNullOrEmpty() ? null : list; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment