最後是工具類用於擴展lambda 污染循環
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Data;
using System.Threading;
namespace Transvalue.Timeline
{
public static class Pollute
{
public static int ToInt32(this string Value)
{
try
{
int a = Int32.Parse(Value);
return a;
}
catch
{
return -1;
}
}
/// <summary>
/// 如果類型值爲Null則返回實例
/// </summary>
/// <param name="Value">類型值</param>
/// <returns></returns>
public static string DecideNull(this string Value)
{
if (Value == null)
{
return "";
}
else
{
return Value;
}
}
/// <summary>
/// 通過字典的值找KEY列表
/// </summary>
/// <typeparam name="T">字典的索引類型</typeparam>
/// <typeparam name="R">字典的值類型</typeparam>
/// <param name="dictionary">查詢字典</param>
/// <param name="Value">查詢的值</param>
/// <returns></returns>
public static List<T> GetKey<T, R>(this Dictionary<T, R> dictionary, R Value)
{
List<T> list = new List<T>();
var a = new Dictionary<Predicate<R>, Action<R>>()
{
{ x => dictionary.Values.Contains(x),s => dictionary.Where(i=>i.Value.Equals(s)).ForEach(b=>list.Add(b.Key))}
};
a.Where(i => i.Key(Value)).ForEach(i => i.Value(Value));
return list;
}
/// <summary>
/// 通過字典索引列表返回字典差集
/// </summary>
/// <typeparam name="T">字典索引類型</typeparam>
/// <typeparam name="R">值的類型</typeparam>
/// <param name="dictionary">字典</param>
/// <param name="Enumerable">對比索引列表</param>
/// <returns></returns>
public static Dictionary<T, R> GetInexistence<T, R>(this Dictionary<T, R> dictionary, IEnumerable<T> Enumerable)
{
var a = dictionary.Keys.Except(Enumerable);
Dictionary<T, R> list = new Dictionary<T, R>();
foreach (var item in a)
{
list.Add(item, dictionary[item]);
}
return list;
}
public static string GetStringReverse(this string str, int Count)
{
string temp = string.Empty;
int i = 0;
while (i < str.Count())
{
var a = str.Skip(i).Take(Count);
a.Reverse<char>().ForEach<char>(c => temp += c);
i += Count;
}
return temp;
}
/// <summary>
/// 對IEnumerable泛型類進行污染,循環訪問IEnumerable泛型類裏的內容,將每個結果發送給指定的方法
/// </summary>
/// <typeparam name="T">類型標識符</typeparam>
/// <typeparam name="R">返回值標識符</typeparam>
/// <param name="source">>IEnumerable泛型數組</param>
/// <param name="func">將值發送給指定的方法,該方法接受一個傳入參數且不返回值,如:Pollute.log()</param>
/// <returns></returns>
public static List<R> ForEach<T, R>(this IEnumerable<T> source, Func<T, R> func)
{
List<R> list = new List<R>();
foreach (var item in source)
{
list.Add(func(item));
}
return list;
}
/// <summary>
/// 對IEnumerable泛型類進行污染,循環訪問IEnumerable泛型類裏的內容,將每個結果發送給指定的方法
/// </summary>
/// <typeparam name="T">類型標識符</typeparam>
/// <typeparam name="R">返回值標識符</typeparam>
/// <param name="source">>IEnumerable泛型數組</param>
/// <param name="func">將值發送給指定的方法,該方法接受一個傳入參數且不返回值,如:Pollute.log()</param>
/// <returns></returns>
public static List<R> ForEach<T, R>(this IEnumerable<T> source, Func<T, R> func, object o)
{
List<R> list = new List<R>();
T t = (T)o;
foreach (T item in source)
{
if (item.GetHashCode() == t.GetHashCode())
{
list.Add(func(item));
}
else
{
list.Add((R)(object)item);
}
}
return list;
}
/// <summary>
/// 對IEnumerable泛型類進行污染,循環訪問IEnumerable泛型類裏的內容,將每個結果發送給指定的方法
/// </summary>
/// <typeparam name="T">類型標示符</typeparam>
/// <param name="source">IEnumerable泛型數組</param>
/// <param name="func">將值發送給指定的方法,該方法接受一個傳入參數且不返回值,如:Pollute.log()</param>
public static void ForEach<T>(this IEnumerable<T> source, Action<T> act)
{
foreach (var item in source)
{
act(item);
}
}
public static void ForEach<T>(this DataColumnCollection DataTable, Action<T> act) where T : class
{
foreach (DataColumn item in DataTable)
{
T t = item as T;
act(t);
}
}
public static void ForEach<T>(this DataRowCollection DataTable, Action<T> act) where T : class
{
foreach (DataRow item in DataTable)
{
T t = item as T;
act(t);
}
}
public static void ForEach(this DataRow DataRow, Action<object> act)
{
foreach (var item in DataRow.ItemArray)
{
act(item);
}
}
/// <summary>
/// 取垂直線的斜率
/// </summary>
/// <param name="f">原始線的斜率</param>
/// <returns></returns>
public static float GetSlope(this float f)
{
return -1 / f;
}
/// <summary>
/// 直角座標轉換爲角座標,返回 0-2pi
/// </summary>
/// <param name="x">x座標。</param>
/// <param name="y">y座標。</param>
/// <returns></returns>
public static double GetAngle(this double x, double y)
{
y = -y;
if (x > 0)
{
if (y > 0)
{
return Math.Atan(y / x);
}
else
{
return 6.28 + Math.Atan(y / x);
}
}
else if (x < 0)
{
return Math.Atan(y / x) + Math.PI;
}
else
{
if (y > 0)
return Math.PI / 2;
else if (y == 0)
return 0;
else
return Math.PI * 1.5;
}
}
public static void log(string msg)
{
Console.WriteLine("{0}: {1}", Thread.CurrentThread.ManagedThreadId, msg);
}
}
}