Linq中委托的实例
List<CustomerRegulationRuleValueChangeDetailList> querychange = (from p in _repository.Change<CustomerRefundChangeValue>().AsQueryable() join d in _repository.Change<CustomerRegulationCabin>().AsQueryable() on p.CustomerRegulationCabinId equals d.Id join e in _repository.Change<CustomerRegulationRule>().AsQueryable() on p.CustomerRegulationRuleId equals e.Id where e.RuleType == 2 && p.CustomerRegulationCabinId == item.Id orderby e.Sort select new CustomerRegulationRuleValueChangeDetailList { TableHead = e.TableHeader, Value = p.Value == "0" ? "免费" : ( GetQueryrefund(p.Value)) }).ToList();
/// <summary> /// <param name="data">正则判断,如果是数子则字符串后加%</param> /// <returns></returns> public static string GetQueryrefund(string data) { if (!string.IsNullOrWhiteSpace(data)) { const string pattern = "^[0-9]*$"; Regex rx = new Regex(pattern); if (rx.IsMatch(data) == true) { return data + "%"; } else { return data; } } else { return ""; } }
根据主键集合删除数据表中的数据,去重
/// <summary> ///主键id集合 /// </summary> public class OutsideFreightRateIds { /// <summary> /// 编号 /// </summary> public List<long?> Ids { get; set; } }
OutsideFreightRateIds data = new OutsideFreightRateIds(); data.Ids = new List<long?>(); data.Ids.add(123); data.Ids.add(456); var listredis = (from a in listredis where DelListByListids(a.Id, data) select a).ToList();
/// <summary> /// 根据泛型集合中的某个属性值集合删除该集合中的元素集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="id"></param> /// <param name="model"></param> /// <returns></returns> private bool DelListByListids<U, T>(U id, T model) { bool result = true; Type t = model.GetType(); var list = t.GetProperties().ToList(); foreach (var itemes in list) { foreach (var item in (IEnumerable)itemes.GetValue(model)) { if (id.ToString() != item.ToString()) { result = true; } else { result = false; return result; }; } } return result; }
定义扩展方法根据某个字段去掉重复
public class Compare<T, C> : IEqualityComparer<T> { private Func<T, C> _getField; public Compare(Func<T, C> getfield) { this._getField = getfield; } public bool Equals(T x, T y) { return EqualityComparer<C>.Default.Equals(_getField(x), _getField(y)); } public int GetHashCode(T obj) { return EqualityComparer<C>.Default.GetHashCode(this._getField(obj)); } }
public static class CommonPareHelper { public static IEnumerable<T> MyDistinct<T, C>(this IEnumerable<T> source, Func<T, C> getfield) { return source.Distinct(new Compare<T, C>(getfield)); } }
方法调用:
var scinfo = list.ToList().MyDistinct(p => p.SensorSerialNum);
或者:
public class LambdaComparer<T> : IEqualityComparer<T> { private readonly Func<T, T, bool> _lambdaComparer; private readonly Func<T, int> _lambdaHash; public LambdaComparer(Func<T, T, bool> lambdaComparer) : this(lambdaComparer, EqualityComparer<T>.Default.GetHashCode) { } public LambdaComparer(Func<T, T, bool> lambdaComparer, Func<T, int> lambdaHash) { if (lambdaComparer == null) throw new ArgumentNullException("lambdaComparer"); if (lambdaHash == null) throw new ArgumentNullException("lambdaHash"); _lambdaComparer = lambdaComparer; _lambdaHash = lambdaHash; } public bool Equals(T x, T y) { return _lambdaComparer(x, y); } public int GetHashCode(T obj) { return _lambdaHash(obj); } }