代码小目
标签: 代码片段 日常记录
日常记录的代码片段
1.使用Paralle进行并行计算累加求和的不同形式
public static int ParallelSum(IEnumerable<int> values)
{
object mutex = new object();
int result = 0;
Parallel.ForEach(
source: values,
localInit: () => 0,
body: (item, state, localValue) => localValue + item,
localFinally: localValue =>
{
lock (mutex)
result += localValue;
});
return result;
}
public static int ParallelSum(IEnumerable<int> values)
{
return values.AsParallel().Sum();
}
public static int ParallelSum(IEnumerable<int> values)
{
return values.AsParallel()
.Aggregate(
seed: 0,
func: (sum, item) => sum + item
);
}
- 值对象的一种实现方式
public abstract class ValueObject<T> : IEquatable<T>
where T : ValueObject<T>
{
public virtual bool Equals(T other)
{
if (other == null)
return false;
var t = GetType();
var otherType = other.GetType();
if (t != otherType)
return false;
var fields = t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
foreach (var field in fields)
{
var value1 = field.GetValue(other);
var value2 = field.GetValue(this);
if (value1 == null)
{
if (value2 != null)
return false;
}
else if (!value1.Equals(value2))
return false;
}
return true;
}
public override bool Equals(object obj)
{
if (obj == null)
return false;
var other = obj as T;
return Equals(other);
}
public override int GetHashCode()
{
var fields = GetFields();
var startValue = 17;
var multiplier = 59;
var hashCode = startValue;
foreach (var field in fields)
{
var value = field.GetValue(this);
if (value != null)
hashCode = hashCode*multiplier + value.GetHashCode();
}
return hashCode;
}
private IEnumerable<FieldInfo> GetFields()
{
var t = GetType();
var fields = new List<FieldInfo>();
while (t != typeof (object))
{
fields.AddRange(t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public));
t = t.BaseType;
}
return fields;
}
public static bool operator ==(ValueObject<T> x, ValueObject<T> y)
{
return x.Equals(y);
}
public static bool operator !=(ValueObject<T> x, ValueObject<T> y)
{
return !(x == y);
}
}
3.ChangeTrackerHelpers
public static class ChangeTrackerHelpers
{
public static void ConvertStateOfNode(EntityEntryGraphNode node) {
IObjectWithState entity = (IObjectWithState)node.Entry.Entity;
node.Entry.State = ConvertToEFState(entity.State);
}
private static EntityState ConvertToEFState(ObjectState objectState) {
EntityState efState = EntityState.Unchanged;
switch (objectState) {
case ObjectState.Added:
efState = EntityState.Added;
break;
case ObjectState.Modified:
efState = EntityState.Modified;
break;
case ObjectState.Deleted:
efState = EntityState.Deleted;
break;
case ObjectState.Unchanged:
efState = EntityState.Unchanged;
break;
}
return efState;
}
}
- 推荐使用查询语法而不是循环
public static IEnumerable<Tuple<int, int>> ProductIndices()
{
for (var i = 0; i < x; i++)
for (var j = 0; j < y; j++)
if (x + y < 100)
yield return Tuple.Create(x, y);
}
/// <summary>
/// 推荐使用查询语法而不是循环
/// </summary>
/// <returns></returns>
public static IEnumerable<Tuple<int, int>> ProductIndicesV2()
{
return from item1 in Enumerable.Range(0, x)
from item2 in Enumerable.Range(0, y)
where item1 + item2 < 100
select Tuple.Create(item1, item2);
}