代码小目

标签: 代码片段 日常记录


日常记录的代码片段

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
        );
}

2. 值对象的一种实现方式

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;
    }
  }

4. 推荐使用查询语法而不是循环

        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);
        }

转载于:https://www.cnblogs.com/Wddpct/p/5909160.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值