最后是工具类用于扩展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);
}
}
}