public static void SetUnionIndex <T>(string indexName, List <string> fields, Attribute.FieldIndexType fieldIndexType) { indexName = string.Format("{0}_{1}", typeof(T).Name, indexName); var indexs = getTableIndex <T>(); if (indexs.UnionIndex.ContainsKey(indexName)) { return; } var unionIndexItem = new UnionIndexItem() { FieldIndexType = fieldIndexType }; for (int i = 0; i < fields.Count(); i++) { var field = fields[i]; if (unionIndexItem.Fields.Contains(field)) { throw new Exception("联合索引 " + indexName + " 中已包括字段" + field); } unionIndexItem.Fields.Add(field); } indexs.UnionIndex.TryAdd(indexName, unionIndexItem); }
/// <summary> /// 设置联合索引 /// </summary> /// <typeparam name="Tresult"></typeparam> /// <param name="indexName"></param> /// <param name="expression"></param> /// <returns></returns> public PropertyBuilder <T> AsUnionIndex <Tresult>(string indexName, Expression <Func <T, Tresult> > expression) { if (string.IsNullOrEmpty(indexName)) { throw new Exception("索引名称是必须的 indexName"); } var type = typeof(T); var newExpression = expression.Body as NewExpression; if (newExpression == null) { throw new Exception("必须为匿名表达式"); } indexName = string.Format("{0}_{1}", typeof(T).Name, indexName); if (indexs.UnionIndex.ContainsKey(indexName)) { return(this); } var unionIndexItem = new UnionIndexItem(); for (int i = 0; i < newExpression.Arguments.Count(); i++) { var item = newExpression.Arguments[i]; MemberExpression m; if (item is UnaryExpression) { var uExp = item as UnaryExpression; m = uExp.Operand as MemberExpression; } else { m = item as MemberExpression; } if (m == null) { throw new Exception(item + "不为MemberExpression"); } if (unionIndexItem.Fields.Contains(m.Member.Name)) { throw new Exception("联合索引 " + indexName + " 中已包括字段" + m.Member.Name); } unionIndexItem.Fields.Add(m.Member.Name); } indexs.UnionIndex.TryAdd(indexName, unionIndexItem); return(this); }