/// <summary>集合相减</summary> /// <param name="entities1">第一个实体集合</param> /// <param name="entities2">第二个实体集合</param> /// <returns></returns> public static EntityList <T> operator -(EntityList <T> entities1, EntityList <T> entities2) { //if (entities1 == null || entities1.Count < 1) return null; if (entities1 == null || entities1.Count < 1) { return(entities1); } EntityList <T> list = new EntityList <T>(); foreach (T item in entities1) { if (entities2 != null && !entities2.Contains(item)) { list.Add(item); } } //if (list == null || list.Count < 1) return null; return(list); }
/// <summary>根据指定项查找字符串。忽略大小写</summary> /// <param name="name">属性名</param> /// <param name="value">属性值</param> /// <returns></returns> public EntityList <T> FindAllIgnoreCase(String name, String value) { //if (Count < 1) return null; if (Count < 1) { return(this); } EntityList <T> list = new EntityList <T>(); foreach (T item in this) { if (item == null) { continue; } if (String.Equals((String)item[name], value, StringComparison.OrdinalIgnoreCase)) { list.Add(item); } } //if (list == null || list.Count < 1) return null; return(list); }
/// <summary>检索与指定谓词定义的条件匹配的所有元素。</summary> /// <param name="match">条件</param> /// <returns></returns> public new EntityList <T> FindAll(Predicate <T> match) { //if (Count < 1) return null; if (Count < 1) { return(this); } EntityList <T> list = new EntityList <T>(); foreach (T item in this) { if (item == null) { continue; } if (match(item)) { list.Add(item); } } //if (list == null || list.Count < 1) return null; return(list); }
/// <summary>根据指定项查找。没有数据时返回空集合而不是null</summary> /// <param name="names">属性名集合</param> /// <param name="values">属性值集合</param> /// <param name="ignoreCase">对于字符串字段是否忽略大小写</param> /// <returns></returns> public EntityList <T> FindAll(String[] names, Object[] values, Boolean ignoreCase = false) { if (Count < 1) { return(this); } FieldItem field = Factory.Table.FindByName(names[0]); if (field != null && (field.IsIdentity || field.PrimaryKey)) { // 唯一键为自增且参数小于等于0时,返回空 if (Helper.IsNullKey(values[0], field.Type)) { return(new EntityList <T>()); } } // 特殊处理字符忽略大小写的情况 var ss = new Boolean[values.Length]; // 特殊处理整数类型,避免出现相同值不同整型而导致结果不同 var ts = new Boolean[values.Length]; var vs = new Int64[values.Length]; for (int i = 0; i < values.Length; i++) { field = Factory.Table.FindByName(names[i]); if (field != null) { ss[i] = field.Type == typeof(String); ts[i] = field.Type.IsIntType(); } if (values[i] == null) { continue; } // 整型统一转为Int64后再比较,因为即使数值相等,类型不同的对象也是不等的 ts[i] |= values[i].GetType().IsIntType(); if (ts[i]) { vs[i] = Convert.ToInt64(values[i]); } ss[i] |= values[i].GetType() == typeof(String); } var list = new EntityList <T>(); for (int k = 0; k < Count; k++) { var item = this[k]; if (item == null) { continue; } var b = true; for (int i = 0; i < names.Length; i++) { var iv = item[names[i]]; if (!Object.Equals(iv, values[i]) && // 整数相等比较 !(ts[i] && Convert.ToInt64(iv) == vs[i]) && // 字符串不区分大小写比较,判定""和null为相等 !(ss[i] && ignoreCase && (iv + "").EqualIgnoreCase(values[i] + ""))) { b = false; break; } } if (b) { list.Add(item); } } return(list); }