/// <summary> /// Join,并返回筛选值 /// </summary> /// <typeparam name="TInner">关联类型</typeparam> /// <param name="expression">关关表达式</param> /// <param name="resultSelector">返回值</param> /// <param name="joinType">join类型,默认Left</param> /// <returns></returns> public LambdaQuery <T> Join <TInner>( Expression <Func <T, TInner, bool> > expression, Expression <Func <T, TInner, object> > resultSelector, JoinType joinType = JoinType.Left) where TInner : IModel, new() { var innerType = typeof(TInner); //TypeCache.SetDBAdapterCache(innerType, dBAdapter); string condition = FormatJoinExpression(expression); var allFilds1 = TypeCache.GetProperties(typeof(T), true); var allFilds2 = TypeCache.GetProperties(innerType, true); var dic = new Dictionary <string, Type>(); foreach (var item in resultSelector.Parameters) { dic.Add(item.Name, item.Type); } QueryFields.Clear(); List <Attribute.FieldAttribute> resultFields = new List <Attribute.FieldAttribute>(); var newExpression = resultSelector.Body as NewExpression; int i = 0; foreach (var item in newExpression.Arguments) { var arry = item.ToString().Split('.'); var aliasName = arry[0]; //like a var type1 = dic[aliasName]; aliasName = GetPrefix(type1); //前缀 Attribute.FieldAttribute f; string name = newExpression.Members[i].Name; if (type1 == innerType)//关联 { //f = allFilds2.Find(b => b.Name == arry[1]); f = allFilds2[arry[1]]; f.AliasesName = name; if (resultFields.Find(b => b.ToString() == f.ToString()) != null) { throw new Exception("不能指定多次相同的字段" + f.Name); } resultFields.Add(f); } else//自已 { //f = allFilds1.Find(b => b.Name == arry[1]); f = allFilds1[arry[1]]; if (QueryFields.Find(b => b.ToString() == f.ToString()) != null) { throw new Exception("不能指定多次相同的字段" + f.Name); } f.AliasesName = name; f.SetFieldQueryScript(GetPrefix(), true, true); QueryFields.Add(f); } i += 1; } AddInnerRelation(innerType, condition, resultFields, true, joinType); return(this); }
public void AddFieldMapping(string field, int location) { if (QueryFields == null) { QueryFields = new Dictionary <string, int>(); } field = field.Split('.').Last().Replace("\"", "").ToUpper(); if (!QueryFields.ContainsKey(field)) { QueryFields.Add(field, location); } }