public static List <FieldRelation> GetFieldRelation(this DataSet dataSource, int tableIndex, string fieldName, string groupCondition = null, string join = null, string joinTableName = null, List <FieldOwn> joinFields = null) { List <FieldRelation> list = new List <FieldRelation>(); DataTable table = dataSource.Tables[tableIndex]; Dictionary <string, FieldAddr> fieldAddres = (Dictionary <string, FieldAddr>)table.ExtendedProperties[TableProperty.FieldAddrDic]; if (fieldAddres.ContainsKey(fieldName.Trim())) //Zhangkj 20170206 增加Trim,避免多余的空格导致的Key不匹配 { FieldAddr addr = fieldAddres[fieldName.Trim()]; FieldRelation fieldRelation = new FieldRelation(table.TableName, table.Columns[addr.FieldIndex].ColumnName, groupCondition); if (string.IsNullOrEmpty(join)) { fieldRelation.Join = string.Empty; fieldRelation.On = null; } else { fieldRelation.Join = join; if (joinFields.Count == 1) { fieldRelation.On = new List <FieldOwn[]>() { new FieldOwn[] { joinFields[0], new FieldOwn(table.TableName, table.PrimaryKey[0].ColumnName) } }; } else { fieldRelation.On = new List <FieldOwn[]>(); string[] pks = GetPkStr(table); for (int i = 0; i < pks.Length; i++) { fieldRelation.On.Add(new FieldOwn[] { joinFields[i], new FieldOwn(table.TableName, pks[i]) }); } } } if (addr.RelSourceIndex == -1) { list.Add(fieldRelation); } else { //为关联字段 bool isLeftJoin = addr.GroupRelIndexs != null; DoFieldRelation(dataSource, table, list, joinTableName, addr.FieldIndex, addr.RelSourceIndex, addr.RelFieldIndex, isLeftJoin); if (isLeftJoin) { foreach (int[] item in addr.GroupRelIndexs) { DoFieldRelation(dataSource, table, list, joinTableName, addr.FieldIndex, item[0], item[1], isLeftJoin); } } } } return(list); }
public static string[] GetFieldBelongTo(this DataSet dataSource, int tableIndex, string fieldName) { DataTable table = dataSource.Tables[tableIndex]; FieldAddr addr = ((Dictionary <string, FieldAddr>)table.ExtendedProperties[TableProperty.FieldAddrDic])[fieldName]; if (addr.RelSourceIndex == -1) { return(new string[] { table.TableName, table.Columns[addr.FieldIndex].ColumnName }); } else { //为关联字段 RelativeSource relSource = null; if (table.Columns[addr.FieldIndex].ExtendedProperties.ContainsKey(FieldProperty.RelativeSource)) { relSource = ((RelativeSourceCollection)table.Columns[addr.FieldIndex].ExtendedProperties[FieldProperty.RelativeSource])[addr.RelSourceIndex]; } DataSet relDataSource = LibSqlModelCache.Default.GetSqlModel(relSource.RelSource); return(GetFieldBelongTo(relDataSource, relSource.TableIndex, relSource.RelFields[addr.RelFieldIndex].Name)); } }